Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

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:
Øyvind Almelid 2010-09-27 15:00:10 +00:00
parent 6a4814c702
commit 210051c407
2 changed files with 48 additions and 10 deletions

View File

@ -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)

View File

@ -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)
@ -43,11 +43,40 @@ class Book(models.Model):
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)