How to use Many-to-many relationships - django

I am using Many-to-many relationships in my app and I am not able to feed data into the table which is by default created by the Django to ensure the Many-to-many relationships.It gives the error in method (def Set_Checkout_Attributes(request):) that 'Customer_check_attributes' object has no attribute 'set_customers' If I replace set_customers with set_users the error will remain same.
The models which I used are:
class Customer(models.Model):
user =models.OneToOneField(User)
birthday =models.DateField()
class Customer_check_attributes(models.Model):
users =models.ManyToManyField(User)
billing_add =models.CharField(max_length=100, blank=True , null=
My view.py is as
def CustomerRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = Registration_Form(request.POST)
if form.is_valid():
user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
user.save()
customer=Customer(user=user, website=form.cleaned_data['website'], birthday=form.cleaned_data['birthday'], store=form.cleaned_data['store'], welcomemail=form.cleaned_data['welcomemail'])
customer.save()
return HttpResponseRedirect('/profile/')
else:
check_form=Check_Attribute_Form()
context={'form':form, 'check':check_form}
return render_to_response('customer/customer_register.html',context , context_instance=RequestContext(request))
else:
''' user is not submitting the form, show them a blank registration form '''
form = Registration_Form()
check_form = Check_Attribute_Form()
context={'form':form,'check':check_form}
return render_to_response('customer/customer_register.html',context , context_instance=RequestContext(request))
####################################### checkout attributes ##################################################
def Checkout_Attributes(request):
check_form = Check_Attribute_Form()
context={'form':check_form}
return render_to_response('customer/checkout.html',context,context_instance=RequestContext(request))
def Set_Checkout_Attributes(request):
#if request.user.is_authenticated():
#return HttpResponseRedirect('/checkout/')
if request.method == 'POST':
check_form = Check_Attribute_Form(request.POST)
#if check_form.is_valid():
customer_check=Customer_check_attributes(billing_add=check_form.data['billing_add'],shipping_add=check_form.data['shipping_add'],payment_method=check_form.data['payment_method'],shipping_method=check_form.data['shipping_method'],reward_points=check_form.data['reward_points'])
customer_check.save()
customer_check.set_customers([user.id])
return HttpResponseRedirect('/profile/')
#else:
#check_form=Check_Attribute_Form()
#return render_to_response('a.html',{'check_form':check_form} , context_instance=RequestContext(request))
else:
return render_to_response('f')
I am got struck here for two days but I can't solve it Please help me.
Thanks

You can use like this:
customer_check.users.add(your user instance)
I think you are trying to use
user.customer_check_set.
but you just use wrongly.
if class x has M2M field y you can reach y directly from x instance like this
x.y
and you can reach x from y like this:
y.x_set
Have fun with django

Related

how to do i automatically set the user field to current user in django modelform

How can i set the current login user to the user field of django model .In my view ,i am using function base view .My model is something like this .
Model.py
class DistributionProfile(Abstract_Class):
Distributortype =(
('B2B1','b2b'),
('b2c','b2c'),
('c2c','c2c'),
('govt4','govt'),
)
ManufacturerType=(('Machine1','Machines'),
('Pharmaceutical2','Pharmaceutical'),
('Jewelries3','Jewelries'),
('Furniture4','Funitures'),
('Electronics5','Electronics'),('Textile6','Textile'),
('Constructionmaterials7','ConstructionHardware'),
('Beverages8','Beverages'),
('Cosmetics9','Cosmetics'),
('Convectionaries10','Convectionaries'),
('AgriculturalProduce11','AgriculturalProduce'),
('RawMaterials12','RawMaterials'),
('CrudOil13','CrudOil'),
('SeaFood14','SeaFood'),
)
title =models.CharField(choices=Distributortype ,null=True,max_length=250,blank=False)
ManufacturerOfInterest =MultiSelectField(choices=ManufacturerType,null=True,blank=False,max_choices=14)
verified = models.BooleanField(default=False,blank=True)
promot=models.BooleanField(default=False,blank=False)
slug = models.SlugField(default='')
user=models.ForeignKey(User,null=True,on_delete=models.CASCADE)
bellow is my form.I would love the form to automatically have the information of the login user filling the form .Meaning the user field should automatically have the information of the login in user .So that i can easily query and display objects created by the login user
form.py
class DistributionProfileForm(forms.ModelForm):
class Meta:
model= DistributionProfile
exclude= ['slug','user','CreatedTime','verified','promot','UpdatedTime']
widgets ={
'CompanyRegisteredName':forms.TextInput(attrs={'class':'distributorform','placeholder':'Name of your company','autofocus':'True'}),
'CompanyRegisteredState':forms.TextInput(attrs={'class':'distributorform','placeholder':' StateOfRegistry'}),
'CompanyRegisteredAddress':forms.TextInput(attrs={'class':'distributorform','placeholder':'Company Address'}),
'CompanyRegisteredCity':forms.TextInput(attrs={'class':'distributorform','placeholder':'Company registered city'}),
'CompanyWebsiteLink':forms.TextInput(attrs={'class':'distributorform','placeholder':'www.mycompany.com'}),
'RegisteredCompanyType':forms.Select(attrs={'class':'distributorform '}),
'Country':forms.Select(attrs={'class':'distributorform'}),
'ManufacturerOfInterest ':forms.CheckboxSelectMultiple(attrs={'class':'multiple_select'}),
}
fields=['CompanyRegisteredName',
'CompanyRegisteredState',
'CompanyRegisteredAddress',
'CompanyRegisteredCity',
'CompanyWebsiteLink',
'RegisteredCompanyType',
'Country','title',
'ManufacturerOfInterest'
]
view.py
def SetUpDistributor(request):
if not request.user:
return HttpResponse('login to access this page ')
if request.method =='POST':
distributor = DistributionProfileForm(request.POST,request.FILES)
if distributor.is_valid():
distributor.save(commit=False)
distributor.user=request.user
distributor.save()
messages.success(request,'Distributor profile created ')
return redirect('gbiz1990:distributor_profile_setup')
else:
messages.error(request,'Something went wrong')
else:
distributor=DistributionProfileForm()
return render(request,"gbiz1990/User_function_pages/distributors.html",{'distributor':distributor})
Your views, may looks like that:
def your_view(request):
form = YourForm(request.POST or None)
if form.is_valid():
your_object = form.save(commit=False)
your_object.user = request.user
your_object.save()
return redirect('your_success_url')
context = {'form': form}
return render(request, 'your_template.html', context)
You need to adapt some parts, but in the general the should be your view. You need to pay attention to that this view needs a login_required decorator in order to not allow non-logged users to create objects.
Use the request.user. I don't know how your app works but when creating, do this:
model = ModelName(user = request.user)
model.save()
I used ModelName because it is best practise to start a class name with a capital, which is not fulfilled in your create model. I suggest you change this.
There also seems to be a mistake in your view, it should be request not reques.
Your view could be like so:
def index(request):
form = FormName(request.POST or None)
if form.is_valid():
form.save()
return render(request, "path/to/template", {"form": form})
And your template could be:
<form method="POST" action="">
{{ form }}
<input type="submit">
</form>
So i solved my problem
in my model.py
class SetdistributorProfile(models.Model):
user=modelss.foreignkey(settings.AUTH_USER_MODEL,null=True,unique=True)
views.py
def SetDistribution(request):
if not requested.user.is_authenticated:
return redirect('app_name:url_name)
if request.method === 'POST':
dis = MymodelForm(request.POST or None)
if dis.is_valid():
instance=dis.save(commit=False)
instance.user=request.user
instance.save()
return redirect('myappname:urlname')
else:
instance=MymodelFomr()
context={'intance':intance}
return render(request,'page.hmtl',context)
Another way to solve this in your views.py
if request.method == "POST":
yourModel = YourModel(user_id = request.user.id)
form = YourModelForm(request.POST, request.FILES, instance=yourModel)
if form.is_valid():
form.save()

Pre-populating Model Form with object data - Django

I have tried various options for this but no luck so far. I am trying to get instance data to be pre-populated into my ModelField. Here is what I have:
forms.py
class edit_project_info(ModelForm):
project_name = forms.CharField(max_length=150)
class Meta:
model = Project
exclude = ['project_type', 'created_date', 'start_date', 'end_date', 'pm_scope', 'dev_scope', 'design_scope', 'testing_scope' ]
View.py
def edit_project (request, offset):
this_project = Project.objects.get(pk=offset)
data = {'project_name' : 'abc'}
if request.method == 'POST':
form = edit_project_info(request.POST, instance=this_project, initial=data)
if form.is_valid():
form.save()
return HttpResponseRedirect('/project_profile/%s/' % offset)
else:
form = edit_project_info()
All I get is an empty field. I can add the initial value to forms.py, but then it is static rather than populated based on the form instance. What I have done here with creating a dict and then passing it to initial in the form instance does not seem to do anything. I'm sure I am missing something basic. Any help would be great! Thanks ahead of time.
Two last lines recreate your form variable. Just remove else: form = edit_project_info():
def edit_project (request, offset):
this_project = Project.objects.get(pk=offset)
data = {'project_name' : 'abc'}
form = edit_project_info(request.POST, instance=this_project, initial=data)
if request.method == 'POST':
if form.is_valid():
form.save()
return HttpResponseRedirect('/project_profile/%s/' % offset)
# else:
# form = edit_project_info()
# ...

Create, get and edit user information in same form and template

I'm trying to retrieve data from user. The form where i want to show the user information is also the same that i use to update this information.
Update3
After some updates I make this work and this is my code. If somenone have a better way to do this can share it :)
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class informacionFacturacion(models.Model):
usuario = models.ForeignKey(User, on_delete=models.CASCADE)
apellidos = models.CharField(max_length=100)
nombres = models.CharField(max_length=100)
[More fields...]
def __str__(self):
self.apellidos
forms.py
from .models import informacionFacturacion
#Create your forms here.
class informacionFacturacionForm(ModelForm):
class Meta:
model = informacionFacturacion
fields = [
"usuario",
"apellidos",
"nombres",
[More fields...]
]
views.py
#login_required
def datosPersonales(request):
#Filter query by user ID
query = informacionFacturacion.objects.filter(usuario=request.user)
form = informacionFacturacionForm()
#If query has content, edit record, else, create a new record
if query:
if request.method == "POST":
form = informacionFacturacionForm(request.POST or None, instance=query[0])
if form.is_valid():
edit_content = form.save()
edit_content.save()
else:
if request.method == "POST":
form = informacionFacturacionForm(request.POST)
if form.is_valid():
create_content = form.save(commit=False)
create_content.save()
return HttpResponseRedirect(reverse('datosPersonales'))
context = {
"titulo": "Datos personales | Co.",
"body_class": "class= sidebar_main_open sidebar_main_swipe",
"form": form,
"infoFacturacion": query,
}
template = "micuenta/datosPersonales.html"
return render(request, template, context)
Thanks for the support.
At first glance, it seems that the informacionFacturacion table is not being populated. Have you checked that the instance.save() is reached? (in other words, that the form is valid)
Second, in the template you want to use the informacionFacturacion object as the form elements, and you are handling them separately. Do:
if request.POST:
form = informacionFacturacionForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
else:
# handle here the form error's, maybe report it in the template
else:
query = informacionFacturacion.objects.filter(usuario=request.user)
form = informacionFacturacionForm(instance=query[0])
and render the form parameter insead of infoFacturacion:
{{ form.as_p }}
finally, make sure that your template form id's matches the form element names, otherwise the form won't be filled.
UPDATE
Based on your edit, now the error is in this line:
form = informacionFacturacionForm(request.POST, instance=query_id)
query_id is an int, and it is expecting a model. Change the following line:
query_id = informacionFacturacion.objects.get(usuario=request.user).id
to
query = informacionFacturacion.objects.get(usuario=request.user)
and the faulty line to:
form = informacionFacturacionForm(request.POST, instance=query)
that should work for now, although code can be simplified a lot.
EDIT 2
Here is what I assume you want:
#login_required
def datosPersonales(request):
query = informacionFacturacion.objects.filter(usuario=request.user)
if request.method == "POST": # This will handle the template form's POST
form = informacionFacturacionForm(request.POST)
if form.is_valid():
asd = form.save(commit=False)
asd.save()
# Here you may want to redirect to somewhere else
# Im not sure here, I guess that you want to handle the GET method if
# there is no form in the request. Post your template form to see what
# is happening.
else:
form = informacionFacturacionForm(instance=query)
# you dont need to save it, it is already in DB
context = {
"titulo": "Datos personales | Co.",
"body_class": "class= sidebar_main_open sidebar_main_swipe",
# I think here is your main issue, you are handling a form object
# AND a infoFacturacion object. You need to use just the
# form object in the template and render it accordingly.
"form": form,
"infoFacturacion": query,
}
template = "micuenta/datosPersonales.html"
return render(request, template, context)
Well, I was with the same problem on my sytem, so I made this solution, maybe it works to you! =D
I'm changing the value of the submit button and using the same form:
<button type="submit" id="submitButton" name="button" value="">Save</button>
If is a new task, I change the value of the button with JQuery:
$('#submitButton').val('new');
And if is an edition, I change the value again:
$('#submitButton').val('edit');
On my views.py, I check if is an edit or a new save by the value of the button:
def index(request):
tasks = Task.object.filter()
context = {
'tasks': tasks
}
if request.method == 'POST':
form = NewTask(request.POST or None)
if request.POST['button'] == 'new':
if form.is_valid():
context['is_valid'] = True
form.save()
form = NewTask()
else:
context['is_valid'] = False
if request.POST['button'] == 'edit':
instance = Task.object.filter(pk=request.POST['id']).first()
form = NewTask(request.POST, instance=instance)
if form.is_valid():
context['is_valid'] = True
form.save()
else:
context['is_valid'] = False
else:
form = NewTask()
context['form'] = form
return render(request, 'index.html', context)

django prepopulate modelform - nothing happens

This must be a very simple thing however I can not seem to get through it..
I trying to build a form where the user can update a ModelForm. First he inserts a user id and afterwards I want to show him the form pre-populate with the original data so he can change only the fields that he wants.
After some the help of my friend google, stackoverflow and the django documentation, I've come to this:
views.py
user = User.objects.get(user_id=usr)
if request.method == 'POST':
form = TableForm(request.POST)
if form.is_valid():
#do something
else:
form = TableForm(instance=user)
return render_to_response('template.html',{'form':form})
forms.py
class TableForm(forms.ModelForm):
pres_clinic = forms.ModelChoiceField(queryset=PresClinic.objects.all(),
widget=SelectWithPop(), label=ugettext("Clinic presentation"),
required=False)
MAYBECHOICES = (
('', '---------'),
(ugettext('Yes'), ugettext('Yes')),
(ugettext('No'), ugettext('No')))
bcg_scar = forms.ChoiceField(choices=MAYBECHOICES, label=ugettext(
"BCG scar"), required=False)
mantoux_register = forms.ChoiceField(choices=MAYBECHOICES,
label=ugettext("Mantoux register"), required=False)
date_diag = forms.DateField(widget=DateTimeWidget, label=ugettext(
"Diagnosis date"), required=False)
situation = forms.ModelChoiceField(queryset=Situation.objects.all(),
widget=SelectWithPop(), label=ugettext("Patient status"),
required=False)
date_situation = forms.DateField(widget=DateTimeWidget, label=ugettext(
"Date patient status"), required=False)
class Meta:
model = Table
fields = ('pres_clinic', 'bcg_scar', 'mantoux_register',
'date_diag', 'situation', 'date_situation')
def clean(self):
cleaned_data = self.cleaned_data
diag = cleaned_data.get('date_diag')
errors = []
now = datetime.date.today()
if diag is not None and diag != u'':
if diag > now:
errors.append(ugettext('The field "Diagnosis date" should be '
'smaller than the actual date'))
if errors:
raise ValidationError(errors)
return cleaned_data
template:
{{ form }} # presents the empty form and not the data from that user
The version of django is 1.4
Can anyone tell me what is wrong and why I'm not able to see the form populated?
Thank you very much
You need to define a dictionary to be used for the initial data and change from TableForm(instance=user) to TableForm(initial=dict), for example something like:
user = User.objects.get(user_id=usr)
if request.method == 'POST':
form = TableForm(request.POST)
if form.is_valid():
#do something
else:
data = {'pres_clinic' : 'value', 'bcg_scar' : 'value', 'mantoux_register' : 'value'}
form = TableForm(initial=data)
return render_to_response('template.html',{'form':form})
I would also put the render to response out of the if statement so if the form isn't valid the page should reload and show any errors.
More information on the django docs here
I hope this helps!
You can try 'model_to_dict'
from django.forms.models import model_to_dict
user = User.objects.get(user_id=usr)
if request.method == 'POST':
form = TableForm(request.POST)
if form.is_valid():
#do something
else:
form = TableForm(initial=model_to_dict(user))
return render_to_response('template.html',{'form':form})

Django form is_valid() fails

I am a real beginner in web development. The following code is failing at the is_valid() check. But I do not understand why: The form should get its data filled from the POST-data or not?
Model:
class Statement(models.Model):
text = models.CharField(max_length=255)
user = models.ForeignKey(User)
time = models.DateField()
views = models.IntegerField()
ModelForm:
class StatementForm(ModelForm):
class Meta:
model = Statement
widgets = {
'time':forms.HiddenInput(),
'user':forms.HiddenInput(),
'views':forms.HiddenInput(),
}
View function:
def new(request):
if request.method == 'POST': # If the form has been submitted...
form = StatementForm(request.POST) # A form bound to the POST data
if form.is_valid():
stmt = form.save()
path = 'stmt/' + stmt.id
return render_to_response(path, {'stmt': stmt})
else:
c = {}
c.update(csrf(request))
loggedin_user = request.user
d = datetime.now()
form = StatementForm(request.POST, initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"), 'user':loggedin_user, 'views':0})
return render_to_response('new_stmt.html', {'form': form, },context_instance=RequestContext(request))
I found similar topics and tried a lot. This is how i think it should work. I really need advice.
All fields of your model are required. So, form.is_valid() will be True, if all fields are filled with correct values and are not blanked.
You have declared fields time, user, views as hidden fields. Are you sure, that you have filled them in your template form?
Also, you may want to auto stamp field time = models.DateField(). Modify your model field like
time = models.DateField(auto_now=True)`.
After this you don't have to fill it by yourself in template form.
Your view must return HttpResponse object in all cases. If your form is not valid, i.e. if form.is_valid() will return False, then no HttpResponse object will be returned by your view. This can be the source of your fail. Add else statement for if form.is_valid():
from django.http import Http404
def new(request):
if request.method == 'POST': # If the form has been submitted...
form = StatementForm(request.POST) # A form bound to the POST data
if form.is_valid():
stmt = form.save()
path = 'stmt/' + stmt.id
return render_to_response(path, {'stmt': stmt})
else:
# Do something in case if form is not valid
raise Http404
else:
# Your code without changes
Change this line:
form = StatementForm(request.POST, initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"), 'user':loggedin_user, 'views':0})
For this:
form = StatementForm(initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"), 'user':loggedin_user, 'views':0})