python - Django Error: solution_building.ID_CUSTOMER_id may not be NULL -


can me this. pretty new , kinda bit blank now. know maybe silly question stuck.

i error:

exception type: integrityerror @ /building/

exception value: solution_building.id_customer_id may not null

what doing wrong here

views.py

class buildingview(updateview): template_name="building.html" model = building form_class = buildingform  def get_context_data(self, **kwargs):     context = super(buildingview, self).get_context_data(**kwargs)        context['pk'] = 1     context['numberofobjects'] = building.objects.all().count()      return context  def get_object(self, queryset = none):     try:          obj = building.objects.get(id = 1)     except:          obj = building.objects.create(id = 1)     return obj  def form_valid(self, form):      form.save()     return httpresponseredirect(reverse("building_view", kwargs={'pk': self.kwargs['pk']})) 

and when doing python manage.py sql got this

create table "solution_building" ( "id" integer not null primary key, "id_customer_id" integer not null references "solution_customer" ("id"), "building_use" varchar(2) not null, "building_floorspace" integer, 

needed help. thank much.

model.py

class building(models.model): id                   = models.autofield(primary_key = true) id_customer          = models.foreignkey(customer) building_use         = models.charfield(max_length = 2, blank = true, choices = c.anvendelse) building_floorspace  = models.integerfield(null = true, blank = true) def __unicode__(self):     return '%s' % (self.id_customer) 

the error here:

def get_object(self, queryset = none):     try:          obj = building.objects.get(id = 1)     except:          obj = building.objects.create(id = 1) <---- here     return obj 

your trying create new building data id?

based on building table, id_customer, building_use, , building_floorspace required value. when trying create new building trigger error.

update:

class building(models.model):     #id = models.autofield(primary_key = true) --> don't need this, system automatically create id     customer = models.foreignkey(customer, null=true, blank=true)     building_use = models.charfield(max_length=2,          blank=true, choices=c.anvendelse, default="base on choices")     building_floorspace  = models.integerfield(default=0)      def __unicode__(self):         return '{0}' % (self) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -