How to insert into multiple tables cascading

35 views
Skip to first unread message

David Crandell

unread,
Jun 22, 2021, 4:23:48 PM6/22/21
to django...@googlegroups.com
Hello, I come from a platform where I built out forms and then manually wrote insert statements from the form input entering all the data at once.

What I want to do is enter a customer (one table), have it populate a master region with the same name (another table), then fill out a master location record (another table) and then create a user from the person profiled in the customer information (another table)

I'm trying to go to the next page which is a location form and pass the value of the customer ID to the region form included on the same page. 

  There has to be a better way to do this. I still have no idea how to combine and enter information into multiple models from one form. The ORM is cool but it seems so limited. I guess I just don't understand it.
class CustomerCreateView(View):
template_name = 'ohnet/customer_form.html'
form_class = CustomerForm
rform = RegionWCust

def get(self, request, *args, **kwargs):
form = self.form_class
rform = self.rform
return render(request, self.template_name, {'form': form, 'rform': rform})

def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, request.FILES)
rform = self.rform(request.POST)
if form.is_valid() and rform.is_valid():
form.save()
obj = Customers.objects.latest('id')
f = rform.save(commit=False)
f.customer_id = obj
f.save()
messages.success(request, f'Customer entered successfully')
return redirect('ohnet:custlocs-new', obj)

DJANGO DEVELOPER

unread,
Jun 22, 2021, 10:47:26 PM6/22/21
to django...@googlegroups.com
please share your related models and form as well.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAACv6dMBaATx9V%3D0t6qrZk2WPVtcVGCOvXwW-aHurzxGE9PB%3Dw%40mail.gmail.com.

David Crandell

unread,
Jun 23, 2021, 9:34:07 AM6/23/21
to Django users
Thank you for your reply.

here are my models. Forms below.

MODELS

class Customers(Prospects):
cust_id = models.IntegerField(default=0)
vertical_cat = models.IntegerField(default=0)
custom_cat = models.IntegerField(default=0)
logo = models.ImageField(default='default.png', upload_to='media/cust-logos/')
max_scripts = models.IntegerField(default=0)
branch_custom_scripts = models.BooleanField(default=False)
receive_emails = models.BooleanField(default=True)
customer_rep_id = models.SmallIntegerField(default=0)
customer_writer_id = models.SmallIntegerField(default=0)
customer_male_vt_id = models.SmallIntegerField(default=0)
customer_fem_vt = models.SmallIntegerField(default=0)
customer_sales_rep = models.SmallIntegerField(default=0)
contract_date_first = models.DateTimeField(default=None, null=True, blank=True)
contract_date_renewal = models.DateTimeField(default=None, null=True, blank=True)
contract_billing_type = models.CharField(max_length=200, null=True, blank=True)
anniversary = models.DateTimeField(default=None, null=True, blank=True)
is_on_contract = models.BooleanField(default=False)
internal_flag = models.IntegerField(default=0)
shuff_freq_pref = models.SmallIntegerField(default=0)
shuff_freq_upd = models.DateTimeField(default=None, null=True, blank=True)
shuff_freq_7 = models.BooleanField(default=True)
shuff_freq_14 = models.BooleanField(default=True)
shuff_freq_30 = models.BooleanField(default=True)
shuff_freq_60 = models.BooleanField(default=True)
shuff_freq_90 = models.BooleanField(default=True)
shuff_freq_180 = models.BooleanField(default=False)
shuff_walk_thru = models.DateTimeField(default=None, null=True, blank=True)
shuff_walk_thru_by = models.SmallIntegerField(models.ForeignKey(Emp, on_delete=models.CASCADE), default=0)
cust_classification = models.ForeignKey(CustomerClasses, on_delete=models.CASCADE)
date_updated = models.DateTimeField(auto_now=True, null=True, blank=True)
updated_by = models.SmallIntegerField(default=0)
audit_note = models.TextField(default=None, null=True, blank=True)
audit_exception = models.BooleanField(default=False)
audit_date = models.DateTimeField(auto_now_add=True)
script_exception = models.BooleanField(default=False)
script_exception_date = models.DateTimeField(default=None, null=True, blank=True)
reports_exception = models.BooleanField(default=False)
reports_exception_date = models.DateTimeField(default=None, null=True, blank=True)
using_script_credits = models.BooleanField(default=False)

def __str__(self):
return self.company_name

def get_absolute_url(self):
return reverse('cust-detail', args=[self.id])


class CustomerRegions(models.Model):
region = models.CharField(max_length=100)
customer = models.ForeignKey(Customers, on_delete=models.CASCADE)

def __str__(self):
return self.region



FORMS

class CustomerForm(forms.ModelForm):
class Meta:
model = Customers
fields = 'company_name', 'contact1_first_name', \
'contact1_last_name', 'email', 'contact2_first_name', \
'contact2_last_name', 'contact2_email', \
'contact2_phone', 'address1', 'address2', \
'city', 'state', 'zip_code', 'phone', \
'mobile', 'web_address', 'notes', \
'cust_classification', 'logo'

class RegionWCust(forms.ModelForm):
class Meta:
model = CustomerRegions
fields = ('region',)

David Crandell

unread,
Jun 23, 2021, 9:35:07 AM6/23/21
to Django users
Sorry customer is inherited from these types

class BaseInfo(models.Model):
contact1_first_name = models.CharField(help_text='Primary Contact First name', max_length=50, default=None)
contact1_last_name = models.CharField(help_text='Primary Contact Last name', max_length=50, default=None)
address1 = models.CharField(help_text='Address 1', max_length=50, default=None)
address2 = models.CharField(help_text='Address 2', max_length=50, default=None, blank=True)
city = models.CharField(help_text='City', max_length=50, default=None)
state = models.CharField(help_text='State', max_length=50, default=None)
zip_code = models.CharField(help_text='Postal/ZIP', max_length=20, default=None)
phone = models.CharField(help_text='Phone', max_length=20, default=None)
email = models.EmailField(default=None)


class Prospects(BaseInfo):
associations = models.CharField(max_length=200, default=None, null=True, blank=True)
company_name = models.CharField(help_text='Company Name', max_length=200, default=None)
contact2_first_name = models.CharField(max_length=50, null=True, blank=True)
contact2_last_name = models.CharField(max_length=50, null=True, blank=True)
contact2_phone = models.CharField(help_text='Phone', max_length=20, default=None, null=True, blank=True)
contact2_email = models.EmailField(default=None, null=True, blank=True)
fax = models.CharField(max_length=20, null=True, blank=True)
mobile = models.CharField(max_length=50, default=None, blank=True, null=True)
web_address = models.CharField(max_length=50, null=True, blank=True)
date_entered = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
date_turned_off = models.DateTimeField(default=None, null=True, blank=True)
notes = models.TextField(help_text='Notes', null=True, blank=True, default=None)

DJANGO DEVELOPER

unread,
Jun 23, 2021, 9:36:54 PM6/23/21
to django...@googlegroups.com
have you tried to implement the inlineformset_factory function within your views? if not then give a try to  inlineformset_factory. because through  inlineformset_factory you can populate data of different models through a single form. for now it looks a solution to me.

David Crandell

unread,
Jun 23, 2021, 11:35:23 PM6/23/21
to django...@googlegroups.com
Thank you my friend. I will give this a try.
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/fP77IUG_KW0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKPY9pmAF_ThEaVt57_cVCYFRft_XnMHnF894Dcc3e%3DVhDbV5A%40mail.gmail.com.

DJANGO DEVELOPER

unread,
Jun 24, 2021, 12:09:05 AM6/24/21
to django...@googlegroups.com
https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/
read this official doc of django. all is written about modelformset and inlineformset_factory.
Reply all
Reply to author
Forward
0 new messages