2016-07-14 9 views
1

Ich habe gelesen und django 1.9 Dokumentation über Multi-Table Vererbung üben und Beziehung umkehren, das ist mein Code:Muti-Tabelle Vererbung und Reverse-Beziehung django

@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 

die Beziehung ManyToManyField für Platz (Parent-Klasse) und suplier (Child-Klasse) wie erwartet funktioniert (liefern „related_name“), aber das Problem ist, wenn ich ein anderes Modell aus einem anderen SubClass (Restaurant) erben machen, wenn ich Consumer Objekte machen will, wie folgt:

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

es ist raise "ValueError: ungültig l Iteral für int() mit Basis 10: '' ". Vollständiger Fehler unten.

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: '' 

Meine Frage ist, ist es möglich, Modell Relation Vererbung von einem anderen SubClass/Child zu machen, wenn es möglich ist, ist mein Code falsch. Übrigens haben die Migrationen für die Modelle keinen Fehler verursacht. Danke für die Antwort und Erklärung, Entschuldigung für mein schlechtes Englisch.

+0

das Problem, wo sonst etwas. Bitte posten Sie den vollständigen Code und den vollständigen Stacktrace – e4c5

+0

@ e4c5 Ich habe meinen Abfragecode hinzugefügt. Vielen Dank –

Antwort

1

Nachdem ich meinen Code versuchen und ändern schließlich kann ich die Consumer Objekte machen, dann ist dies der Code, die ich geändert wurden (i hinzufügen, ein id-Attribut in Place Klasse da Fehler in Autofield Klasse happends ini __init__.py Linie 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 

Vielleicht hat jemand darüber hinaus erklären kann, ist der Mechanismus der ID-Feld in Multi-Table Vererbung