generic-foreign-key

Django admin - sort by GenericForeignKey's field

ⅰ亾dé卋堺 提交于 2019-12-24 01:24:44
问题 I need sort functionality on a GenericForeignKey's field on list display. models.py class DealPayment(models.Model): product_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() deal = GenericForeignKey('product_type', 'object_id') # This deal can be of 'product' or 'certificate' payment_date = models.DateTimeField(blank=True, null=True, help_text="Date when payment is done.") transaction_amount = models.DecimalField(max_digits=16, decimal_places=2, default=0, help

django: how do I query based on GenericForeignKey's fields?

人盡茶涼 提交于 2019-12-18 10:05:12
问题 I'm new in using GenericForeignKey, and I couldn't make it to work in a query statement. The tables are roughly like the following: class Ticket(models.Model): issue_ct = models.ForeignKey(ContentType, related_name='issue_content_type') issue_id = models.PositiveIntegerField(null=True, blank=True) issue = generic.GenericForeignKey('issue_ct', 'issue_id') class Issue(models.Model): scan = models.ForeignKey(Scan) A scan creates one issue, an issue generates some tickets, and I made Issue as a

Django Create and Update views: foreign key field

天大地大妈咪最大 提交于 2019-12-11 05:11:37
问题 Suppose I have the two following classes : class Parcel(models.Model): name = models.CharField(max_length=NAME_MAX_LENGTH) garden = models.ForeignKey(Garden, on_delete=models.CASCADE) def __str__(self): return self.name class Bed(models.Model): parcel = models.ForeignKey(Parcel, on_delete=models.CASCADE) name = models.CharField(max_length=NAME_MAX_LENGTH) length = models.IntegerField() width = models.IntegerField() I'm using Django's generic views to Create and Update new beds. As parcel is a

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

陌路散爱 提交于 2019-12-08 19:51:17
问题 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

Generic Relations/Generic Foreign Keys in the Django Admin

陌路散爱 提交于 2019-12-03 13:04:18
问题 I've been trying to display a Generic Foreign Key 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

django: prefetch related objects of a GenericForeignKey

心不动则不痛 提交于 2019-12-03 08:54:15
问题 Suppose I have a model Box with a GenericForeignKey that points to either an Apple instance or a Chocolate instance. Apple and Chocolate , in turn, have ForeignKeys to Farm and Factory , respectively. I want to display a list of Box es, for which I need to access Farm and Factory . How do I do this in as few DB queries as possible? Minimal illustrative example: class Farm(Model): ... class Apple(Model): farm = ForeignKey(Farm) ... class Factory(Model): ... class Chocolate(Model): factory =

Generic Relations/Generic Foreign Keys in the Django Admin

别说谁变了你拦得住时间么 提交于 2019-12-03 03:18:12
I've been trying to display a Generic Foreign Key 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 Supported Programs, with the option of creating a new one. Is this possible? Where am

How to traverse a GenericForeignKey in Django?

吃可爱长大的小学妹 提交于 2019-11-30 09:13:11
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')

django: how do I query based on GenericForeignKey's fields?

懵懂的女人 提交于 2019-11-29 19:43:20
I'm new in using GenericForeignKey, and I couldn't make it to work in a query statement. The tables are roughly like the following: class Ticket(models.Model): issue_ct = models.ForeignKey(ContentType, related_name='issue_content_type') issue_id = models.PositiveIntegerField(null=True, blank=True) issue = generic.GenericForeignKey('issue_ct', 'issue_id') class Issue(models.Model): scan = models.ForeignKey(Scan) A scan creates one issue, an issue generates some tickets, and I made Issue as a foreign key to Ticket table. Now I have a Scan object, and I want to query for all the tickets that