【问题标题】:Django: Finding out which table in inheritance hierarchy has the fieldDjango:找出继承层次结构中的哪个表具有该字段
【发布时间】:2011-08-02 11:03:52
【问题描述】:

考虑以下示例:

class Base(models.Model):
    myfield = models.CharField()
class Derived(Base):
    pass

现在,基类和派生类将在数据库中拥有不同的表。

我的问题是如何找出myfield属于哪个表?

【问题讨论】:

标签: django inheritance model field


【解决方案1】:

使用_meta.get_fields_with_model()方法:

for field, model in Derived._meta.get_fields_with_model():
    if field.name == 'myfield':
        model = model or Derived
        print 'myfield belongs to %s' % model._meta.db_table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多