New to using Django over here. I am trying to create a DJango form that will save data to the database. To do this, I am using CreateView
When calling the URL that is to "do the work", I have it set up as follows (so, a parameter is being passed in as well)
url(r'^owner/contacts/add/(?P<tenantid>[0-9]+)/$', views.MstrstoreheadcontactCreateView.as_view(), name='owner-contact-add'),
Problem:
When trying to save data to the DB, I get an error that seems to be due to some fields not being set properly
the exact error message I am getting is:
The above exception (ORA-02291: integrity constraint (ORAAPPS.SYS_C009216) violated - parent key not found) was the direct cause
This is how the Model is defined:
class Mstrstoreheadcontact(models.Model):
tenantid = models.ForeignKey('Mstrauthowner', models.DO_NOTHING, db_column='tenantid', blank=True, null=True)
contactid = models.BigIntegerField(primary_key=True)
genderid = models.BigIntegerField(blank=True, null=True)
firstname = models.CharField(max_length=20, blank=True, null=True)
lastname = models.CharField(max_length=20, blank=True, null=True)
officephoneno = models.CharField(max_length=20, blank=True, null=True)
cellphoneno = models.CharField(max_length=20, blank=True, null=True)
class Meta:
managed = False
db_table = 'MstrStoreHeadContact'
it was created using the inspectdb
option (used to auto-generate classes for tables that already exist in a database)
This is how the CreateView is being defined in views.py
class MstrstoreheadcontactCreateView( CreateView ):
model = Mstrstoreheadcontact
fields = [ 'firstname', 'lastname', 'officephoneno', 'cellphoneno']
def form_valid(self, form):
self.kwargs['tenantid'] = 10
# returning rendered page
super(MstrstoreheadcontactCreateView, self).form_valid(form)
return render(self.request, self.template_name,
self.get_context_data(form=form))
def get_context_data(self, **kwargs):
ctx = super(MstrstoreheadcontactCreateView, self).get_context_data(**kwargs)
ctx['tenantid'] = self.kwargs['tenantid']
return ctx
My understanding was
that the attributes defined with "fields" were the ones that were to be seen on the form. Yet, when doing a "save", the other fields look as though they are being ignored (as seen in the error log).
Right now, self.kwargs['tenantid'] = 10
is being set manually in the CreateView
code. How can I fix this so that it will take a value that is passed with the URL. Why does it look as though the fields being set to None on insert?
Update
@dirkgroten - thanks for the response! I added the function as instructed. This is the error that I got.
TypeError at /masterdata/owner/contacts/add/10/ __init__() got an unexpected keyword argument 'tenantid'
Request Method: GET
Request URL: http://127.0.0.1:8000/masterdata/owner/contacts/add/10/
Django Version: 1.11.1
Exception Type: TypeError
Exception Value: __init__() got an unexpected keyword argument 'tenantid'
I saw this as well: Getting __init__() got an unexpected keyword argument 'instance' with CreateView of Django
How to set ForeignKey in CreateView?
Do I need to create a form to go with the view?