天道酬勤,学无止境

generic-foreign-key

In Django/South HOWTO create an instance of a model from a different app during DataMigration

I need to perform a datamigration of a model Answer in app Question. In that script there is a dependency such that I need to create an instance of a model Chapter which is in the app Journal. So, I coded it as follows: def forwards(self, orm): for answer_object in orm.Answer.objects.all(): #This Works. blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100]) blog.save() #This DOES NOT work chapter, is_created = orm['journal.Chapter'].objects.get_or_create(content_object=blog) chapter.save() #cleanup task, not relevant to this question below answer_object

2021-07-12 11:29:11    分类:问答    django   instantiation   data-migration   django-south   generic-foreign-key

如何在 Django 中遍历 GenericForeignKey?(How to traverse a GenericForeignKey in Django?)

问题 我正在使用 Django v1.9.4,后面是 PostgreSQL 9.2.14。 使用以下型号: from django.db import models from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey from django.contrib.contenttypes.models import ContentType class Foo(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() bar = GenericForeignKey('content_type', 'object_id') class Bar(models.Model): foos = GenericRelation(Foo, related_query_name='bars') class Meta: abstract = True class BarX(Bar): name = models.CharField(max_length=10, default='bar x') class BarY(Bar): name =

2021-06-23 22:49:45    分类:技术分享    python   django   postgresql   generic-foreign-key   generic-relations

Django 管理中的通用关系/通用外键(Generic Relations/Generic Foreign Keys in the Django Admin)

问题 我一直试图在 Django 管理员中显示一个GenericForeignKey ,但无法让它工作。 我有一个FullCitation类,可以链接到NonSupportedProgram或SupportedProgram类。 所以,我使用了通用外键。 在管理员中,我希望用户只能从content_type下拉列表中选择'NonSupportedProgram'或'SupportedProgram' ,然后从object_id字段中,我需要用户能够从列出现有NonSuportedProgram或现有的SupportedProgram s,可以选择创建一个新的。 这可能吗? 我哪里错了? 模型.py class FullCitation(models.Model) # the software to which this citation belongs # either a supported software program or a non-supported software program limit = models.Q(app_label = 'myprograms', model = 'supportedprogram') | models.Q(app_label = 'myprograms', model = 'nonsupportedprogram')

2021-06-21 13:09:46    分类:技术分享    python   django   django-models   django-admin   generic-foreign-key

Error Using CheckConstraint in Model.Meta along with Django GenericForeignKey - Joined field references are not permitted in this query

I am trying to restrict GFK to be pointed to objects of a few models only, and I thought CheckConstraint will be a great way to do this, however I get this error class ManualAdjustment(Model): content_type = models.ForeignKey(ContentType, null=True, on_delete=models.CASCADE) object_id = models.PositiveIntegerField(null=True) booking_obj = GenericForeignKey('content_type', 'object_id') # should point to a app1.Booking1 or app2.Booking2 or app3.Booking3 only - trying to enforce this via CheckConstraint class Meta: constraints = [ models.CheckConstraint( check= Q(content_type__app_label='app1'

2021-05-26 05:46:40    分类:问答    django   django-migrations   django-postgresql   generic-foreign-key   django-generic-relations

Generic Relations/Generic Foreign Keys in the Django Admin

I've been trying to display a GenericForeignKey in the Django admin but can't get it working. I have a FullCitation class that can be linked to either a NonSupportedProgram or a SupportedProgram class. So, I have used a generic foreign key. In the admin, I want users to only be able to select 'NonSupportedProgram' or 'SupportedProgram' from the content_type dropdown and then, from the object_id field, I need users to be able to select from a dropdown listing the existing NonSuportedPrograms or the existing SupportedPrograms, with the option of creating a new one. Is this possible? Where am I

2021-05-25 04:55:26    分类:问答    python   django   django-models   django-admin   generic-foreign-key

sqlalchemy通用外键(例如在django ORM中)(sqlalchemy generic foreign key (like in django ORM))

问题 sqlalchemy是否具有django的GenericForeignKey之类的东西? 使用通用外国字段是否正确。 我的问题是:我有几个模型(例如,Post,Project,Vacancy,那里没有什么特别的),我想为每个模型添加注释。 我只想使用一种注释模型。 值得吗? 还是应该使用PostComment,ProjectComment等? 两种方法的优缺点? 谢谢! 回答1 我最常使用的最简单的模式是,每个关系实际上都有单独的注释表。 乍一看这似乎很可怕,但是与使用任何其他方法相比,它不会招致任何其他代码-这些表是自动创建的,并且使用模式Post.Comment , Project.Comment等来引用模型。评论保留在一个地方。 从引用的角度来看,这种方法是最简单,最有效的方法,也是最DBA友好的方法,因为不同类型的注释保存在自己的表中,这些表可以单独调整大小。 要使用的另一种模式是单个注释表,但有不同的关联表。 这种模式提供了一个用例,您可能希望同时将Comment链接到一种以上的对象(例如同时发布和项目)。 这种模式仍然相当有效。 第三,有多态关联表。 此模式使用固定数量的表来表示集合和相关类,而不会牺牲引用完整性。 这种模式试图与Django风格的“通用外键”最接近,同时仍保持引用完整性,尽管它不像前两种方法那样简单。 也可以模仿ROR / Django使用的模式

2021-04-27 14:41:13    分类:技术分享    python   django   sqlalchemy   generic-foreign-key

How to traverse a GenericForeignKey in Django?

I'm using Django v1.9.4 with PostgreSQL 9.2.14 behind. With the following models: from django.db import models from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey from django.contrib.contenttypes.models import ContentType class Foo(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() bar = GenericForeignKey('content_type', 'object_id') class Bar(models.Model): foos = GenericRelation(Foo, related_query_name='bars') class Meta: abstract = True class BarX(Bar): name = models.CharField(max_length=10, default='bar x')

2021-04-24 17:29:32    分类:问答    python   django   postgresql   generic-foreign-key   generic-relations

sqlalchemy generic foreign key (like in django ORM)

Does sqlalchemy have something like django's GenericForeignKey? And is it right to use generic foreign fields. My problem is: I have several models (for example, Post, Project, Vacancy, nothing special there) and I want to add comments to each of them. And I want to use only one Comment model. Does it worth to? Or should I use PostComment, ProjectComment etc.? Pros/cons of both ways? Thanks!

2021-04-12 14:27:28    分类:问答    python   django   sqlalchemy   generic-foreign-key