Пример #1
0
class Person(models.Model):
    name = models.CharField(max_length=20)
    friends = ArrayManyToManyField('self')
    idols = ArrayManyToManyField('self', symmetrical=False, related_name='stalkers')

    def __str__(self):
        return self.name
class Article(models.Model):
    headline = models.CharField(max_length=50)
    pub_date = models.DateTimeField()
    primary_categories = ArrayManyToManyField(Category, related_name='primary_article_set')
    secondary_categories = ArrayManyToManyField(Category, related_name='secondary_article_set')

    class Meta:
        ordering = ('pub_date',)

    def __str__(self):
        return self.headline
class AbstractArticle(models.Model):
    class Meta:
        abstract = True

    publications = ArrayManyToManyField(Publication,
                                        name='publications',
                                        related_name='+')
class Book(models.Model):
    title = models.CharField(max_length=255)
    authors = ArrayManyToManyField(Author, related_name='books')

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['id']
class Reader(models.Model):
    name = models.CharField(max_length=50)
    books_read = ArrayManyToManyField(Book, related_name='read_by')

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['id']
class Author2(models.Model):
    name = models.CharField(max_length=50, unique=True)
    first_book = models.ForeignKey('Book', models.CASCADE, related_name='first_time_authors+')
    favorite_books = ArrayManyToManyField('Book', related_name='+')

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['id']
class Article(models.Model):
    headline = models.CharField(max_length=100)
    # Assign a unicode string as name to make sure the intermediary model is
    # correctly created. Refs #20207
    publications = ArrayManyToManyField(Publication, name='publications')

    def __str__(self):
        return self.headline

    class Meta:
        ordering = ('headline',)
class Teacher(models.Model):
    name = models.CharField(max_length=50)
    qualifications = ArrayManyToManyField(Qualification)

    objects = TeacherManager()

    def __str__(self):
        return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))

    class Meta:
        ordering = ['id']
class Person(models.Model):
    name = models.CharField(max_length=50)
    houses = ArrayManyToManyField(House, related_name='occupants')

    @property
    def primary_house(self):
        # Assume business logic forces every person to have at least one house.
        return sorted(self.houses.all(), key=lambda house: -house.rooms.count())[0]

    @property
    def all_houses(self):
        return list(self.houses.all())

    class Meta:
        ordering = ['id']
class Product(models.Model):
    name = models.CharField(max_length=15)
    keywords = ArrayField(models.CharField(max_length=20), default=[], form_size=10, blank=True)
    sports = ArrayField(models.CharField(max_length=20),default=[], blank=True, choices=(
    ('football', 'Football'), ('tennis', 'Tennis'), ('golf', 'Golf'), ('basketball', 'Basketball'), ('hurling', 'Hurling'), ('baseball', 'Baseball')))
    shipping = HStoreField(keys=('Address', 'City', 'Region', 'Country'), blank=True, default={})
    details = JSONField(fields=details_fields, blank=True, default={})
    buyers =  ArrayManyToManyField(Buyer)

    def __str__(self):
        return self.name

    @property
    def country(self):
        return self.shipping.get('Country', '')
class Bio(models.Model):
    author = models.OneToOneField(Author, models.CASCADE)
    books = ArrayManyToManyField(Book, blank=True)
class BookWithYear(Book):
    book = models.OneToOneField(Book, models.CASCADE, parent_link=True)
    published_year = models.IntegerField()
    aged_authors = ArrayManyToManyField(
        AuthorWithAge, related_name='books_with_year')
class Flea(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    current_room = models.ForeignKey(Room, models.SET_NULL, related_name='fleas', null=True)
    pets_visited = ArrayManyToManyField(Pet, related_name='fleas_hosted')
    people_visited = ArrayManyToManyField(Person, related_name='fleas_hosted')
class Pet(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=20)
    people = ArrayManyToManyField(Person, related_name='pets')
class Department(models.Model):
    name = models.CharField(max_length=50)
    teachers = ArrayManyToManyField(Teacher)

    class Meta:
        ordering = ['id']
class Array(models.Model):
    index = models.IntegerField()
    numbers = ArrayManyToManyField(NumberArray, db_index=False)

    def __str__(self):
        return self.index