简体   繁体   中英

IntegrityError at /admin/app/price_list/add/ (1048, “Column 'business_id_id' cannot be null”)

i am working with django to create three tables that define a store, it's item list and a price list. In the 'price_list' model i created Foreign key links to the other two tables that i later use to create a compisite primary key using the unique together option. It all validates without an error but when i try to change the 'price_list' via the admin interface, i get the error as stated above in the title to this post. Editing for the other two models goes on without a glitch. As i understand it, the error is saying the foreign key linked to the primary key of the businesses table is null. How can that be when the value is automatically generated?

Please help

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)

I suspect you're missing a capital B in the foreign key definition for "businessID" on Price_List.

I'm not sure but I think the whole "related_name='businessID'" argument is unnecessary since you're wanting to create a foreign key to the primary key of the Businesses table.

Try it - it might work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM