多表继承和反向关系django

时间:2016-07-14 22:21:41

标签: python django django-models django-inheritance

我一直在阅读和练习关于多表继承和反向关系的django 1.9文档,这是我的代码:

@python_2_unicode_compatible
class Place(models.Model):
    name=models.CharField('Restaurant Name',max_length=50,db_column='name of restaurant')
    address=models.CharField('Restaurant Address',max_length=50,db_column='address of restaurant')

    def __str__(self):
        return '%s and %s' % (self.name,self.address)

@python_2_unicode_compatible
class Restaurant(Place):
    serves_hot_dogs=models.BooleanField(default=False)
    serves_pizza=models.BooleanField(default=False)
    owner=models.CharField(max_length=50)
    parent_link=models.OneToOneField(Place,parent_link=True,default="",null=False)

    def __str__(self):
        return self.owner

@python_2_unicode_compatible
class Suplier(Place):
    customers=models.ManyToManyField(Place,related_name='provider')
    suplier_name=models.CharField(max_length=50)

    def __str__(self):
        return self.suplier_name

@python_2_unicode_compatible
class Consumer(Restaurant):
    myrestaurant=models.ForeignKey(Restaurant,null=True,related_name='restaurant_consumer')
    consumer_name=models.CharField(max_length=50,default=1)

    def __str__(self):
        return self.consumer_name

Place(父类)和Suplier(子类)的关系ManyToManyField按预期工作(提供“related_name”)但问题是当我从另一个SubClass(餐厅)继承另一个模型时,当我想要生成Consumer对象如下:

>>> from multitableinheritance.models import Place,Restaurant,Suplier,Consumer
>>> Consumer.objects.all()
[]
>>> Consumer.objects.create(consumer_name='Andrew')

它引发了“ValueError:int()的无效文字,基数为10:''”。完全错误如下。

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 401, in create
    obj.save(force_insert=True, using=self.db)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 708, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 735, in save_base
    self._save_parents(cls, using, update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 759, in _save_parents
    self._save_parents(cls=parent, using=using, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 760, in _save_parents
    self._save_table(cls=parent, using=using, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 801, in _save_table
    forced_update)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 831, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1203, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1099, in build_lookup
    return final_lookup(lhs, rhs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 19, in __init__
    self.rhs = self.get_prep_lookup()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 57, in get_prep_lookup
    return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 744, in get_prep_lookup
    return self.get_prep_value(value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

我的问题是,是否有可能从另一个SubClass / ChildClass进行模型关系继承,如果有可能我的代码错误。顺便说一下,模型的迁移没有引起任何错误。谢谢你的回答和解释,抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

在我尝试修改我的代码后,我可以制作消费者对象,这是我修改过的代码(我在Place类中添加了一个id属性,因为错误发生在Autofield类ini __init__.py第976行)

@python_2_unicode_compatible
class Place(models.Model):
    name=models.CharField('Restaurant Name',max_length=50,db_column='name of restaurant')
    address=models.CharField('Restaurant Address',max_length=50,db_column='address of restaurant')
    #---> add Place Class attribute id <---
    id=models.PositiveIntegerField(default=1,primary_key=True,unique=True)

    def __str__(self):
        return '%s and %s' % (self.name,self.address)

@python_2_unicode_compatible
class Restaurant(Place):
    serves_hot_dogs=models.BooleanField(default=False)
    serves_pizza=models.BooleanField(default=False)
    owner=models.CharField(max_length=50)
    parent_link=models.OneToOneField(Place,parent_link=True,default="",null=False)

    def __str__(self):
        return self.owner

@python_2_unicode_compatible
class Suplier(Place):
    customers=models.ManyToManyField(Place,related_name='provider')
    suplier_name=models.CharField(max_length=50)

    def __str__(self):
        return self.suplier_name

@python_2_unicode_compatible
class Consumer(Restaurant):
    myrestaurant=models.ForeignKey(Restaurant,null=True,related_name='restaurant_consumer',default=1)
    consumer_name=models.CharField(max_length=50,default="")

    def __str__(self):
        return self.consumer_name

也许有人可以进一步解释,多表继承中id字段的机制