I've extended the django user with a OneToOneField so I can store address and such.
SiteUser is the model which extends User using a OneToOneField. How can I get fields of both User and SiteUser in a single ModelForm?
Here is the relevant code so far:
class ProfileForm(ModelForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')
class AddressForm(ModelForm):
pass
View in question:
def edit_profile(request):
username = request.user
user = User.objects.get(username__exact=username)
profileform_class = ProfileForm
if request.method == 'POST':
profileform = profileform_class(data=request.POST, instance=user)
if profileform.is_valid():
profileform.save()
return redirect('profile')
else:
profileform = profileform_class(instance=user)
return render(request, 'edit_profile.html', {
'user': user,
'profileform': profileform,
})
And the two models:
class Product(models.Model):
order = models.IntegerField(default=0)
name = models.CharField(max_length=255)
description = models.TextField()
image = models.ImageField(upload_to='product-images', default='default.jpg')
price = models.FloatField()
slug = models.SlugField(unique=True)
class SiteUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=255)
post_number = models.CharField(max_length=255, default='')
post_location = models.CharField(max_length=255, default='')
HTML Page I want the forms on:
{% extends 'base.html' %}
{% block title %}
Rediger {{ product.name }} - {{ block.super }}
{% endblock title %}
{% block content %}
<h1>Rediger "{{ user }}"</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ profileform.as_p }}
{{ addressform.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock content %}
One option is to use inline formsets. Using this, you won't be needing a second ModelForm.
Inline formsets is a small abstraction layer on top of model formsets. These simplify the case of working with related objects via a foreign key.
You can find good examples here.
Alternatively, if you want to avoid inline formsets and use both ProfileForm and AddressForm under a single <form> tag as you have done in your template, you can do it like this.
Forms:
class ProfileForm(ModelForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')
class AddressForm(ModelForm):
class Meta:
model = SiteUser
exclude = ['user']
Views:
def edit_profile(request):
username = request.user
user = User.objects.get(username__exact=username)
profileform_class = ProfileForm
addressform_class = AddressForm
if request.method == 'POST':
profileform = profileform_class(data=request.POST, instance=user)
addressform = addressform_class(data=request.POST, instance=user.siteuser)
if all((profileform.is_valid(), addressform.is_valid())):
user = profileform.save()
address = addressform.save(commit=False)
address.user = user
address.save()
return redirect('profile')
else:
profileform = profileform_class(instance=user)
addressform = addressform_class(instance=user.siteuser)
return render(request, 'edit_profile.html', {
'user': user,
'profileform': profileform,
'addressform': addressform,
})
I don't know much about forms, but I think you should use the "initial" parameter when instantiating the AddressForm, as exemplified here: https://docs.djangoproject.com/es/1.9/topics/forms/modelforms/#providing-initial-values
So you create your AddressForm class with SiteUser as model, and when you instantiate it in the view, you make it like this:
AddressForm(initial={'user': request.user})
If "username" is not the primary key of the User model, you can get the primary key like this:
User.objects.get(username=request.user).pk
and then give it in the "initial" parameter.
model:
class Company(models.Model):
name = models.CharField(max_length=150)
description = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Bike(models.Model):
to_company = models.OneToOneField(Company, on_delete=models.CASCADE, related_name="bike_company")
name_bike = models.CharField(max_length=150)
model = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
form.py
class CompanyForm(ModelForm):
class Meta:
model = Company
fields = ("name", "description" )
class BikeForm(ModelForm):
name_bike = forms.CharField(required=True)
model = forms.CharField(required=True)
class Meta(CompanyForm.Meta):
model = Company
#transaction.atomic
def save(self):
company = super().save(commit=False)
company.name = self.cleaned_data.get("name")
company.description = self.cleaned_data.get("description")
company.save()
bike = Bike.objects.create(to_company=company)
bike.name_bike = self.cleaned_data.get("name_bike")
bike.model = self.cleaned_data.get("model")
bike.save()
return company
the relationship is kept in this line:
Bike.objects.create(to_company=company)
here is an example of a different type of user model
models user and type
https://gist.github.com/josuedjh3/259b4b3b03ab195637fe2db3c701edd6
FormModel the User and UserCreationForm
https://gist.github.com/josuedjh3/0c26d989552a82d5b252c5bd3fed1054
Related
I have a "project" model that has a "status" field. The status can be active, paused, or complete. I want to be able to update the field via form on the project detail view.
I have read a few solutions to this problem but, as a newbie, I haven't been able to get this to work. When I submit the form I get an http 405 error and the instance is not updated.
the model:
class Project(models.Model):
title = models.CharField(max_length= 200)
description = tinymce_models.HTMLField()
status = models.CharField(max_length=20, choices=PROJECT_CHOICES, default="active")
date = models.DateTimeField(auto_now_add=True, null=True)
created_by = models.ForeignKey(CustomUser, editable=False, null=True, blank=True, on_delete=models.RESTRICT)
objects = ProjectManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('company_project:project_detail', args=[str(self.id)])
the view
class CompanyProjectsDetailView(DetailBreadcrumbMixin, FormMixin, DetailView):
model = Project
id = Project.objects.only('id')
template_name = 'company_accounts/project_detail.html'
context_object_name = 'project'
form_class = ProjectStatusForm
notescount = Project.objects.annotate(num_notes=Count('notes'))
documentscount = Project.objects.annotate(num_documents=Count('project_documents'))
todoscount = Project.objects.annotate(num_documents=Count('todo_group'))
def form_valid(self, form):
project = get_object_or_404(Project, id=self.kwargs.get('pk'))
theform = form.save(commit=False)
theform.project = project
form.save()
return super(CompanyProjectsDetailView, self).form_valid(form)
the form
class ProjectStatusForm(forms.ModelForm):
class Meta:
model = Project
fields = ['status']
labels = {'status': 'project status'}
widgets = {
'status': forms.Select(attrs={'id':'PROJECT_CHOICES'}),
}
On the page I use this code to add the form
<form action="" method="post">
{% csrf_token %}
{{ form.media }}
{{ form|crispy }}
</br>
<input type="submit" value="save">
</form>
I have a model called 'Competicion', with some objects and another model called 'Participante'. This second model has two fields: a foreignkey with the user and another foreingkey to 'Competicion'.
In the view, I've made queryset from 'Competicion' and with a for loop in the template I've given each object the button of the form.
With storing the user of the current session I have no problem but I want the form to know which object of the queryset it is to grab its id. #I have no problem with choices I just don't include them to save space
Models.py
class Competicion(models.Model):
ciudad = models.CharField(max_length=50, null=True, choices=Ciudades_a_elegir, default="Ninguna")
nombre = models.CharField(max_length=20, null=True, choices=Competiciones_a_elegir, default="Ninguna")
titulo = models.CharField(max_length=40, null=True)
fecha = models.DateField(null=True)
participantes = models.IntegerField(null=True)
flyer = models.ImageField(null=True, upload_to='imagenes', default='Ninguna')
def __str__(self):
return self.nombre + " " + self.titulo
class Participante(models.Model):
competicion = models.ForeignKey(Competicion, on_delete=models.CASCADE, null=True, blank=True)
participantes = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.competicion.nombre + " " + self.competicion.titulo
forms.py
class ParticipanteForm(ModelForm):
class Meta:
model = Participante
exclude = ['competicion', 'participantes']
views.py
def competiciones(request):
qs = Competicion.objects.all()
form = ParticipanteForm(request.POST or None)
if form.is_valid():
nombres = form.save()
nombres.competicion = ???#Help me
nombres.participantes = request.user
nombres.save()
return redirect('home')
context = {
'object_list':qs,
'form':form,
}
return render(request, "competiciones.html", context)
template
{% for i in object_list %}
<ul>
<li>{{i.nombre}}</li>
<li>{{i.ciudad}}</li>
<li>{{i.titulo}}</li>
<li>{{i.fecha}}</li>
<li>{{i.participantes}}</li>
{% if i.flyer %}
<img src="{{i.flyer.url}}" width="100px" >
{% endif %}
<li><form action="" method="POST">
{% csrf_token %}
<p> {{ form.competicion}} </p>
<p>{{form.participantes}}</p>
<input type="submit" name="Inscribirse" value="Inscribirse">
</form> </li>
</ul>
{% endfor %}
This is only like 1 course. In number one there is the different fields of the Competicion Model. And number two is the button of the form to the Participante Model, which fields are hidden and take the id of the course and the user. So I have a lot of these both courses displayed in the web. The function of the Particpante Model is to store the people who register in the course and the course itself.
def competiciones(request):
qs = Competicion.objects.all()
form = ParticipanteForm(request.POST or None)
if form.is_valid():
data = form.save()
data.participantes_id = request.user
for i in qs:
data.competicion_id = i.id
data.save()
return redirect('home')
I'm currently learning Django forms and I came across this post.
One of the forms currently looks like this:
What I'd like to do is to change Category into a formset and be able to render multiple dropdowns while creating a product.
My models.py:
class Category(models.Model):
name = models.CharField(max_length=30)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=30)
price = models.DecimalField(decimal_places=2, max_digits=10)
category = models.ForeignKey(Category, on_delete = models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
My forms.py:
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ('name', )
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('name', 'price', 'category', )
def __init__(self, user, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.fields['category'].queryset = Category.objects.filter(user=user)
Current method in views.py:
#login_required
def new_product(request):
if request.method == 'POST':
form = ProductForm(request.user, request.POST)
if form.is_valid():
product = form.save(commit=False)
product.user = request.user
product.save()
return redirect('products_list')
else:
form = ProductForm(request.user)
return render(request, 'products/product_form.html', {'form': form})
products_form.html:
{% extends 'base.html' %}
{% block content %}
<h1>New product</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="save">
cancel
</form>
{% endblock %}
What I tried is to make use of the modelformset_factory and change the method in views.py by creating a CategoryFormSet as:
CategoryFormSet = modelformset_factory(Category, fields=('name', ), extra=2)
formset = CategoryFormSet(data=data, queryset=Category.objects.filter(user=request.user))
then replacing the original form from views.py with the created formset. In the html I simply replace the {{form}} with {{formset}}. After playing around with it for a while, I either get the New product with just a submit button (no form rendered) or a User object has no attribute GET error. What am I doing wrong?
The tutorial focuses on allowing the user to add/update more instances of one model. You want to edit one thing, with multiple related things inline.
However, your data model only allows one category per product, so this does not make any sense. Whether you want more than one category per product, is something only you can answer :) - I'm going to assume you want that.
First you need to change your model to allow for multiple categories per product:
class Product(models.Model):
name = models.CharField(max_length=30)
price = models.DecimalField(decimal_places=2, max_digits=10)
categories = models.ManyToManyField(Category, related_name='products')
user = models.ForeignKey(User, on_delete=models.CASCADE)
And then you need to learn about Inline Formsets.
Come back with a specific if you get stuck on that.
Instead of creating new model Category. You can do this.
CATEGORY_CHOICES= (
("1", "1"),
("2", "2"),
("3", "3"),
("4", "4"),
("5", "5"),
("6", "6"),
("7", "7"),
("8", "8"),
)
category = models.CharField(max_length = 20,choices = CATEGORY_CHOICES,default = '1')
It will automatically render in HTML.
I have Contestant model that has many-to-one relationship with Category. I want the creator of the award to only have access to last 15 categories of the award he has created in instantiating contestant model. That's, the multichoice queryset in the Contestant field(category) will only show the list of the last 15 categories being created in the Category Model. I have made different efforts, but the code is either not working or giving '['ManagementForm data is missing or has been tampered with']' error.
I have made countless re-factorization of codes and I have tried to adopt solutions I found on internet. But they didn't work.
# MY CATEGORY MODEL
class Category(models.Model):
award = models.ForeignKey(Award, on_delete=models.CASCADE)
award_category = models.CharField(max_length=150, blank=True, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def __str__(self):
return self.award_category
# MY CONTESTANT MODEL
class Contestant(models.Model):
award_name = models.ForeignKey(Award, on_delete=models.CASCADE)
contestant_name = models.CharField(max_length=150, null=True, blank=True )
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='contestants')
vote = models.IntegerField()
def number_of_vote(self):
return Contestant.objects.filter(self.vote).count()
vote_count = property(number_of_vote)
#MY VIEW
def get_award(request, pk):
new_award = get_object_or_404(Award, pk=pk)
Contest_Formset = modelformset_factory(Contestant, fields('contestant_name', 'category',), extra=15)
formset = Contest_Formset(request.POST)
for form in formset:
form.fields['category'].queryset = Category.objects.filter(user=request.user)[1:15]
if request.method == 'POST' and form.is_valid():
myform = form.save(commit=False)
myform.award_name = new_award
myform.save()
return redirect('award_details', pk=new_award.pk)
else:
formset = Contest_Formset()
context = {
'new_award': new_award,
'formset': formset
}
return render(request, 'voting/get_award.html', context)
# TEMPLATE
<form action=" " method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{{ formset }}
<input type="submit" value="save">
</form>
I expect the output of the contestant's category field to show only the last 15 categories created by the login user.
I wrote template
<div class="container">
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
</div>
And, my View is below
class UserProfileUpdateView(UpdateView):
model = UserProfile
form_class = UserProfileUpdateForm
template_name_suffix = '_update_form'
And this is my UserProfile model below.
It uses User key
class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User)
# The additional attributes we wish to include.
website = models.URLField(blank=True, verbose_name=u'Webサイト')
picture = models.ImageField(
upload_to='uploads/profile_images/%Y/%m/%d', blank=True,
null=True, verbose_name=u'Image'
)
age = models.IntegerField(verbose_name=u'Age')
country = models.CharField(max_length=30, verbose_name=u'country')
profile = models.CharField(max_length=300, verbose_name=u'profile')
address = models.CharField(max_length=300, verbose_name=u'address')
GENDER_CHOICES = (
(u'M', u'Male'),
(u'F', u'Female'),
)
gender = models.CharField(max_length=2, choices=GENDER_CHOICES, verbose_name=u'Gender')
CATEGORY_CHOICES = (
(u'U', u'u'),
(u'R', u'r'),
)
category = models.CharField(max_length=2, choices=CATEGORY_CHOICES, verbose_name=u'category')
last_accessed = models.DateTimeField()
My Form is
class UserProfileUpdateForm(forms.ModelForm):
slug_field = 'user_slug'
class Meta:
model = UserProfile
fields = (
'address', 'profile', 'gender', 'category',
'age',
'website', 'picture',
)
But, my form can't fill default value which already set before.
Could you tell me why doesn't set?
You should look at this: Django: using ModelForm to edit existing database entry
The key is:
form = MyModelForm(instance=MyModel.objects.get(pk=some_id))