1

私は2つのモデルを持っています:

class First(models.Model):
    name = models.CharField(max_length=50, default='n/a')
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()


    def __unicode__(self):
        return str(self.pk) + ' > ' + self.name


class Second(models.Model):
    name = models.CharField(max_length=50, default='n/a')
    r = generic.GenericRelation(First)

    def __unicode__(self):
        return str(self.pk) + ' > ' + self.name

私がする時:

from myapp.models import First
First._meta.get_all_field_names()

私は得る:

['content_type', u'id', 'name', 'object_id', 'second']

の代わりにcontent_object、ここにGenericForeignKey名前が付けられているようsecondです。期待される動作ですか?

ps
Django 1.5.1を使用しています。

4

2 に答える 2

1

https://django-model-internals-reference.readthedocs.org/en/latest/get_all_field_names.html

get_all_field_names
このモデルで可能なすべてのフィールド名のリストを返します (逆リレーション名を含む)。これは、デバッグ出力 (選択肢のリスト) をきれいに印刷するために使用されるため、内部専用のフィールド名は含まれません。

この場合、逆リレーション名はsecondです。はい、予想される動作です。

于 2013-06-10T16:53:50.323 に答える
1

AGenericForeignKeyは舞台裏で2 つのフィールドを使用します。この場合content_typeそれらはデフォルトの名前であるためobject_idです。によって作成されました。secondGenericRelationSecond.r

于 2013-06-10T16:50:35.230 に答える