Django: Using Class Based Views How Do I create a form that uses two models? -


i'm using class based generic views because presented in django official tutorial. have 2 related models:

class client(models.model):     first_name = models.charfield(max_length=40)     last_name = models.charfield(max_length=40)     email = models.emailfield     phone_regex = regexvalidator(regex=r'^\+?1?\d{9,15}$', message="phone number must entered in format: '+999999999'. 15 digits allowed.")     phone_number = models.charfield(validators=[phone_regex], blank=true, max_length=15) # validators should list        def __str__(self):             return self.first_name + self.last_name  class clientaddress(models.model):     client = models.foreignkey(client, on_delete=models.cascade)     street = models.textfield()     city = models.textfield()     state = models.textfield()     zip_code = models.textfield() 

i have 1 view creating new client object:

class clientcreateview(generic.createview):     model = client     form_class = clientform      success_url = reverse_lazy('work:client_index') 

i have 1 form each model:

class clientform(modelform):     class meta:         model = client         fields = ['first_name', 'last_name']   class clientaddressform(modelform):     class meta:         model = clientaddress         fields = ['street', 'city', 'state', 'zip_code',] 

and have 1 template rendering clientcreateview:

<h1>create client form</h1> <form action="" method="post">{% csrf_token %}     {{ form.as_p }}     {{ sub_form.as_p }}     <input type="submit" value="save" /> </form> 

i want generate associated client , clientaddress objects using single form or load both django forms in single template gets saved @ once or whatever other methodology correct. how do this?


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -