我正在使用django创建三个定义商店的表,它的商品列表和价目表。在'price_list'模型中,我创建了指向其他两个表的外键链接,我后来使用这些表来使用unique unique选项创建compisite主键。这一切都在没有错误的情况下验证,但是当我尝试通过管理界面更改“price_list”时,我会收到上述帖子标题中所述的错误。对其他两个型号的编辑继续进行,没有任何故障。 据我了解,错误是说与business表的主键链接的外键为null。如何自动生成值?
请帮忙
models.py
from django.db import models
class Businesses(models.Model):
business_name = models.CharField(max_length=50, unique=True)
telephone_number = models.CharField(max_length=15)
where = models.ManyToManyField('Location', verbose_name='where?')
def __unicode__(self):
return self.business_name
class Meta:
ordering = ['business_name']
class Location(models.Model):
located_at = models.CharField(max_length=30)
city = models.CharField(max_length=30)
country = models.CharField(max_length=30)
def __unicode__(self):
return u'%s' % (self.located_at)
class Meta:
ordering = ['located_at']
class Item_list(models.Model):
item_name = models.CharField(max_length=50, unique=True)
def __unicode__(self):
return self.item_name
class Meta:
ordering = ['item_name']
class Price_list(models.Model):
price_list_id = models.AutoField(primary_key=True)
price = models.IntegerField(max_length=50)
business_id = models.ForeignKey(Businesses, related_name='businessID')
item_id = models.ForeignKey(Item_list, related_name='ItemID')
business_name = models.ForeignKey(Businesses, to_field='business_name', related_name='businessName')
item_name = models.ForeignKey(Item_list, to_field='item_name', related_name='itemName')
def __unicode__(self):
return u'%s' % self.price
class Meta:
ordering = ['price']
unique_together = (("price_list_id", "business_id", "item_id"),)
admin.py
from django.contrib import admin
from biaSearch.app.models import *
class BusinessAdmin(admin.ModelAdmin):
list_display = ('business_name', 'telephone_number')
search_field = ('business_name')
filter_horizontal = ('where',)
class LocationAdmin(admin.ModelAdmin):
list_display = ('located_at', 'city', 'country')
search_field = ('located_at')
list_filter = ('located_at', 'city')
class Item_listAdmin(admin.ModelAdmin):
list_display = ('item_name',)
class Price_listAdmin(admin.ModelAdmin):
list_display = ('price',)
fields = ('price', 'business_name', 'item_name')
admin.site.register(Businesses, BusinessAdmin)
admin.site.register(Location, LocationAdmin)
admin.site.register(Item_list, Item_listAdmin)
admin.site.register(Price_list, Price_listAdmin)
答案 0 :(得分:0)
我怀疑你在Price_List的“businessID”的外键定义中错过了大写B.
我不确定,但我认为整个“related_name ='businessID'”参数是不必要的,因为您想要为Businesses表的主键创建外键。
尝试一下 - 它可能会有效!