Gjort om Id til en klasse med en en-til-en-relasjon til book, og sørget for at id har funksjonen getid og setid.
This commit is contained in:
parent
6a4814c702
commit
210051c407
|
@ -1,7 +1,7 @@
|
|||
from web.library.models import *
|
||||
from django.contrib import admin
|
||||
|
||||
mod = [Category, BookSeries, ReferenceType, Reference, Book, AlternativeTitle, Copy, Person, Relation, BookPerson, Placement]
|
||||
mod = [Category, BookSeries, ReferenceType, Reference, Book, AlternativeTitle, Copy, Person, Relation, BookPerson, Placement, Id]
|
||||
|
||||
for model in mod:
|
||||
admin.site.register(model)
|
||||
|
|
|
@ -22,7 +22,7 @@ class Reference(models.Model):
|
|||
|
||||
class Book(models.Model):
|
||||
isbn = models.CharField(max_length=13, primary_key=True)
|
||||
id = models.CharField(max_length=255)
|
||||
# id = models.CharField(max_length=255)
|
||||
title = models.CharField(max_length=511)
|
||||
subtitle = models.CharField(max_length=511, null=True, blank=True)
|
||||
category = models.ForeignKey(Category, null=True, blank=True)
|
||||
|
@ -35,19 +35,48 @@ class Book(models.Model):
|
|||
picture = models.ImageField(upload_to='%Y/%m/%d/pictures', null=True, blank=True)
|
||||
thumbnail = models.ImageField(upload_to='%Y/%m/%d/thumbnails', null=True, blank=True)
|
||||
references = models.ManyToManyField(Reference, related_name='books')
|
||||
|
||||
|
||||
def full_print(self):
|
||||
for field in book_fields.items():
|
||||
try:
|
||||
print '%-15s: %50s' % (field[1],eval('self.'+field[0]))
|
||||
except Category.DoesNotExist:
|
||||
print '%-15s: %50s' % (field[1], 'Does not exist')
|
||||
|
||||
class Meta:
|
||||
unique_together=(("isbn","id"),)
|
||||
|
||||
def getid(self):
|
||||
try:
|
||||
return self.id.id
|
||||
except Id.DoesNotExist:
|
||||
return None
|
||||
|
||||
def setid(self,newid):
|
||||
try:
|
||||
id = self.id
|
||||
id.delete()
|
||||
newid = Id(book=self,id=newid)
|
||||
newid.save()
|
||||
|
||||
except Id.DoesNotExist:
|
||||
id=Id(id=newid,book=self)
|
||||
id.save()
|
||||
# return Book.get(isbn=self.isbn)
|
||||
|
||||
|
||||
def __unicode__(self):
|
||||
return self.title
|
||||
|
||||
# def print(self):
|
||||
# print '%13s %5s %30s %'
|
||||
|
||||
# class Meta:
|
||||
# unique_together=(("isbn","id"),)
|
||||
|
||||
class Id(models.Model):
|
||||
id = models.CharField(max_length=511, primary_key=True)
|
||||
book = models.OneToOneField(Book)
|
||||
|
||||
class AlternativeTitle(models.Model):
|
||||
book = models.ForeignKey(Book)
|
||||
book = models.ForeignKey(Book, related_name='alt_titles')
|
||||
alt_title = models.CharField(max_length=511)
|
||||
|
||||
class Copy(models.Model):
|
||||
|
@ -59,16 +88,25 @@ class Person(models.Model):
|
|||
id = models.CharField(max_length=255, primary_key=True)
|
||||
first_name = models.CharField(max_length=255)
|
||||
last_name = models.CharField(max_length=255)
|
||||
references = models.ManyToManyField(Reference, related_name='persons')
|
||||
references = models.ManyToManyField(Reference, related_name='persons', null=True, blank=True)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.first_name + u' ' + self.last_name
|
||||
|
||||
class Relation(models.Model):
|
||||
name = models.CharField(max_length=31)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
class BookPerson(models.Model):
|
||||
book = models.ForeignKey(Book)
|
||||
person = models.ForeignKey(Person)
|
||||
book = models.ForeignKey(Book, related_name='person')
|
||||
person = models.ForeignKey(Person, related_name='books')
|
||||
relation = models.ForeignKey(Relation)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.person.first_name +u' '+ self.person.last_name+ u' '+ self.relation.name+ u' of '+self.book.title
|
||||
|
||||
class Placement(models.Model):
|
||||
category = models.ForeignKey(Category)
|
||||
shelf = models.CharField(max_length=10)
|
||||
|
|
Reference in New Issue