I am using CreateView to update my Person model. That model has a onetoone relation with Adress. When trying to update my Person model I cannot update the Adress model at the same time. It just gives me a list of existing addresses, but no way to add an new address. I can see that this is possible in admin. is it possible to this in th CreateView?
model.py:
class Person(Model):
fname=CharField(default="missing",max_length=100)
lname=CharField(default="missing",max_length=100)
mobil=PhoneField(default='9999999999')
mail=EmailField(default='contact@gmail.com')
padress=OneToOneField(Adress,on_delete=CASCADE,primary_key=True)
def __str__(self):
return self.fname
class Meta:
ordering=('fname','lname')
class Adress(Model):
street=CharField(default='missing',max_length=100)
snumb=CharField(default='missing',max_length=15)
town=CharField(default='missing',max_length=100)
postn=CharField(default='99999',max_length=5,validators=[postnvali])
def __str__(self):
return self.town
class Meta:
ordering=('street','town')
view.py
class PCreate(CreateView):
template_name='kammem/create.html'
model=Person
fields=['fname','lname','mobil','mail','padress']
success_url=reverse_lazy('forl')
url.py
path('pcreate/',PCreate.as_view(),name='pcreate'),
template:
{% extends 'edit2.html' %}
{% block add %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
{% endblock %}
any suggestions?
ok, here is an update. I can save both instances and also create the relation. But I still can't exit the function, please look at the following:
class PCreate2(CreateView):
form_class=PersonForm
template_name='kabmmem/create.html'
success_url=reverse_lazy('forl')
def form_valid(self,form):
# create address and person
street = form.cleaned_data['street']
snumb = form.cleaned_data['snumb']
town = form.cleaned_data['town']
postn = form.cleaned_data['postn']
fname = form.cleaned_data['fname']
lname = form.cleaned_data['lname']
mobil = form.cleaned_data['mobil']
mail = form.cleaned_data['mail']
a=Adress.objects.create(street=street,snumb=snumb,town=town,postn=postn)
pe=Person.objects.create(fname=fname,lname=lname,mobil=mobil,mail=mail,padress=a)
return HttpResponseRedirect(self.get_success_url())
The return statement gives: AttributeError: 'NoneType' object has no attribute 'dict'
so, I am still lost...
the return statement