Wednesday, June 23, 2021

Re: How to insert into multiple tables cascading

Thank you my friend. I will give this a try.

On Wed, Jun 23, 2021 at 8:36 PM DJANGO DEVELOPER <abubakarbryar@gmail.com> wrote:
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.

On Wed, Jun 23, 2021 at 6:35 PM David Crandell <guitardave8077@gmail.com> wrote:
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)

On Wednesday, June 23, 2021 at 8:34:07 AM UTC-5 David Crandell wrote:
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',)
On Tuesday, June 22, 2021 at 9:47:26 PM UTC-5 abubak...@gmail.com wrote:
please share your related models and form as well.

On Wed, Jun 23, 2021 at 1:22 AM David Crandell <guitard...@gmail.com> wrote:
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)

--
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/92e546fe-24cd-4ff8-a5b8-9fa7c5e1c9fcn%40googlegroups.com.

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAACv6dP9zZEJ5jdvHAXy%3DwHkQxjwor17VZEGNbfL3nVz%2B9rFVQ%40mail.gmail.com.

No comments:

Post a Comment