简体   繁体   English

如何根据具有相同字段值的对象总数在 model 中添加自动增量字段?

[英]How can I add an auto increment field in a model based on total count of objects with same field value?

I'm new to the whole Django thing and a bit lost.我是整个 Django 的新手,有点迷茫。 Sorry if the title is a bit confusing I'll try to clear things out.抱歉,如果标题有点混乱,我会尝试清除内容。

So basically I have two models ( Folder and Document ).所以基本上我有两个模型( Folder 和 Document )。 A Document can have a single Folder as one of its fields using a Foreign Key . Document可以使用Foreign Key将单个Folder作为其字段之一。 Now I have another field in Document that needs to get the value of the total Document objects that share the same Folder and increase by one.现在我在Document中有另一个字段,需要获取共享同一文件夹的Document 对象总数并增加一个。

I've tried things I read on the docs ( aggregation, F() objects, overriding model's save() function ) as well as some answers is read here but didn't manage to get it right so I'm posting to get some help.我已经尝试过我在文档上阅读的内容(聚合、F() 对象、覆盖模型的 save() function )以及在此处阅读了一些答案但未能正确理解,所以我发布了一些答案帮助。 Below is my models.py file with the two models and some comments for better understanding.下面是我的 models.py 文件,其中包含两个模型和一些注释,以便更好地理解。

class Folder(models.Model):
    category = models.IntegerField()
    subCategory = models.IntegerField()
    name = models.CharField(max_length= 50)
    desc = models.TextField()

class Document(models.Model):
    folder = models.ForeignKey(Folder, on_delete=models.CASCADE)
    date_created = models.DateField()
    date_added = models.DateTimeField()

    #The field below needs to sum all Document objects that share 
    #the same folder value in the database + 1 and set it as its default value
    f_no = models.IntegerField(default=lambda: Document.objects.aggegate(Count('folder')) + 1)

Thank you in advance, any leads or clues are most welcome提前谢谢您,欢迎提供任何线索或线索

EDIT: Forgot to say that all management is done via Django's admin dashboard if this has anything to do at all with my situation.编辑:忘了说所有管理都是通过 Django 的管理仪表板完成的,如果这与我的情况有任何关系的话。 I registered both models in admin.py and that's all.我在 admin.py 中注册了这两个模型,仅此而已。 I make new Folder objects when needed and save Documents with one specific Folder in them each time我在需要时创建新的文件夹对象,并每次保存带有一个特定文件夹的文档

I would recommend creating a ManyToMany relation in the Folder, and add a created Document object into the Folder's ManyToMany relation.我建议在文件夹中创建多对多关系,并将创建的文档 object 添加到文件夹的多对多关系中。

Models.py模型.py

class Folder(models.Model):
    category = models.IntegerField()
    subCategory = models.IntegerField()
    name = models.CharField(max_length= 50)
    desc = models.TextField()
    documents = models.ManyToManyField('app.Document')

You can add can add documents to the folder by using .add() to the ManyToMany relation and the amount of documents in the relation by using .count()您可以通过使用.add() .count()中的文档数量来添加可以添加到文件夹的文档

ManyToMany relations are well documented here: https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_many/多对多关系在这里有很好的记录: https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_many/

When you create a Document model object, it represents a single item.当您创建文档 model object 时,它表示单个项目。 That single item shouldn't have a count of how many documents are in a file.该单个项目不应该计算文件中有多少文档。 The f_no you note should actually be in the Folder model.您记下的f_no实际上应该在文件夹 model 中。

Once you create a Document object that is related to a Folder object via ForeignKey, you can use signals to increment the f_no field that resides in the Folder object.通过 ForeignKey 创建与文件夹 object 相关的文档 object 后,您可以使用信号来递增位于文件夹 object 中的f_no字段。

@receiver(post_save, sender=Document)
def increment_folder_item_count(sender, **kwargs):
    # get Folder object via Document model instance folder foreignkey field
    # folder.f_no += 1
    # folder.save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算实际总字段的总值并将此值存储在 odoo 中 model 的另一个字段中? - How can I calculate total value of actual total field and store this value in another field in model in odoo? 如何计算Django中相同模型的不同字段的总出现次数? - How to count total occurrence of distinct field of same model in django? 如何通过在旁边添加另一个字段来自动增加 django 管理表单中的 model 字段? - How to auto increment a model field in django admin form by having add another on the side? 如何基于同一模型中其他字段的值设置Django模型字段值 - How to set django model field value based-on value of other field in the same Model 如何根据总数的百分比拆分字段值 - How to split field value based on percentage of total 如何获取模型的相关对象和模型的子项相关对象的总计数? - How can I get a total count of a model's related objects and the model's children's related objects? 如何使用同一模型的IntegerField将天数添加到Django模型的日期时间字段中? - How can i add days to a datetime field on a django model, using a IntegerField of the same model? 保存期间视图集中 model 的自动增量字段 - Auto increment field of model in viewset during saving 我如何添加与其他模型相关的选择字段并传递 id 当前对象? - How i can add choice field related to other model and pass id current objects? 如何使用 Django Model 中的输入字段值自动生成字段值? - How to auto generate a field value with the input field value in Django Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM