如何匹配模型上的特定字段?

时间:2019-04-25 18:51:42

标签: django django-models

我的模型如下,我需要以 ForeignKey 的身份获得 bookname ,并且它可以在Django Admin上运行,但我还需要与以下内容相关的 pageCount bookPageCount 可以在Django Admin或添加新记录时自动确定。当我将新日志添加到 ReadList 时,我希望 pageCount 根据从 Book ;

中选择的记录自动定义
class Book(models.Model):
    bookname = models.CharField(max_length=200, verbose_name='Kitap Adı')
    bookAuthor = models.CharField(max_length=100, verbose_name='Yazar Adı')
    bookPublisher = models.CharField(max_length=100, verbose_name='Yayın Evi')
    bookPageCount = models.PositiveIntegerField(verbose_name='Sayfa Sayısı')
    bookAddDate = models.DateField(verbose_name='Kitaplığa Eklenme Tarihi')
    class Meta:
        verbose_name = 'Kitap'
        verbose_name_plural = 'Kitaplar' 

class ReadList(models.Model):
    bookName = models.ForeignKey('kitaplik.Book.bookname', related_name='bookName', on_delete=models.CASCADE, verbose_name='Kitap Adı')
    readerName = models.ForeignKey('ogrenciler.Students.studentName', on_delete=models.CASCADE, related_name='readerName', verbose_name='Okuyan Kişi')
    dateOfRead = models.DateField(verbose_name='Okuma Tarihi')
    pageCount = models.ForeignKey('kitaplik.Book.bookPageCount', related_name='pageCount', on_delete=models.PROTECT, verbose_name='Sayfa Sayısı')
    class Meta:
        verbose_name = 'Okuma Günlüğü'

第一个答案后,我将 post_save 接收者导入到 models.py 中,现在是这样;

class Book(models.Model):
    bookname = models.CharField(max_length=200, verbose_name='Kitap Adı')
    bookAuthor = models.CharField(max_length=100, verbose_name='Yazar Adı')
    bookPublisher = models.CharField(max_length=100, verbose_name='Yayın Evi')
    bookPageCount = models.PositiveIntegerField(verbose_name='Sayfa Sayısı')
    bookAddDate = models.DateField(verbose_name='Kitaplığa Eklenme Tarihi')
    class Meta:
        verbose_name = 'Kitap'
        verbose_name_plural = 'Kitaplar'
    def __str__(self):
        return self.bookname

class ReadList(models.Model):
    bookName = models.ForeignKey('Book', related_name='book', on_delete=models.CASCADE, verbose_name='Kitap Adı')
    readerName = models.ForeignKey('ogrenciler.Students', on_delete=models.CASCADE, related_name='readerName', verbose_name='Okuyan Kişi')
    dateOfRead = models.DateField(verbose_name='Okuma Tarihi')
    pageCount = models.PositiveIntegerField(verbose_name='Sayfa Sayısı',blank=True, null=True)
    class Meta:
        verbose_name = 'Okuma Günlüğü'
@receiver(post_save, sender=ReadList)
def get_pageCount(sender, instance, **kwargs):
    instance.pageCount = instance.book.pageCount
    instance.save

1 个答案:

答案 0 :(得分:0)

也许可以使用ForeignKey编写更简洁的代码,而不必重复这些字段。

class ReadList(models.Model):
    book = models.ForeignKey('kitaplik.Book', related_name='book', on_delete=models.CASCADE, verbose_name='Kitap Adı')
    readerName = models.ForeignKey('ogrenciler.Students.studentName', on_delete=models.CASCADE, related_name='readerName', verbose_name='Okuyan Kişi')
    dateOfRead = models.DateField(verbose_name='Okuma Tarihi')
        class Meta:
        verbose_name = 'Okuma Günlüğü'

接下来,您可以在视图或模板中使用ReadList.book.pageCount

编辑 如果要将book.pageCount的值作为单独的值存储在ReadList对象中,则可以在每次创建ReadList之后启动一个信号:

@receiver(post_save, sender=ReadList)
def get_pageCount(sender, instance, **kwargs):
    instance.pageCount = instance.book.pageCount
    instance.save