I'm trying to add an instance to the Books model, which is added using the inheriting form of ModelForm, the Books instance is added, but I'm not able to relate the user field to the User model.
forms.py
from django import forms
from .models import Books
class BooksCreationForm(forms.ModelForm):
class Meta:
model = Books
fields = ('title',
'language', 'status', 'short_desc',
)
models.py
User = settings.AUTH_USER_MODEL
class Books(models.Model):
STATUS_CHOICES = (
('Reading', 'Reading'),
('Finished', 'finished'),
)
user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='books',null=True)
title = models.CharField(max_length=120)
short_desc = models.TextField(max_length=1000)
slug = AutoSlugField(populate_from='title', overwrite=True)
language = models.CharField(max_length=20, blank=True)
status = models.CharField(max_length=35, choices=STATUS_CHOICES)
views.py
class AddBooksView(View):
# ....
def post(self, request, *args, **kwargs):
template = self.template
form = self.form(request.POST)
# ?? user = User.objects.get(request.user)
user = User.objects.get(username=request.user.username)
if form.is_valid():
form.save(commit=False)
form.user = user
form.save()
print("book post sucess")
return redirect('main:books')
context = {'form': self.form()}
return render(request, template, context)
But when i add in the shell, works:
>>> u = User.objects.get(username='marcos')
>>> u.email
'marcos#marcos.com'
>>> bk = Books.objects.create(title='stackoverflow', language='Python', status='reading', short_desc='desc...')
>>> bk.title
'stackoverflow'
>>> bk.user = u
>>> bk.user.username
'marcos'
>>> bk.save()
>>> for v in Books.objects.filter(user=u):
... print(v.title, v.user.username)
...
stackoverflow marcos
several instances that i try save through ModelForm have user = None:
>>> for v in Books.objects.all():
... print(v.user, v.title)
...
None Pyhon2
None dfdsffffff
None fgdfdfd
None fgdfgfg
marcos stackoverflow # that i add before using shell
I also try removing user in the view and using this in the form:
class BooksCreationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
return super(BooksCreationForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
kwargs['commit'] = False
instance = super(BooksCreationForm, self).save(*args, **kwargs)
if self.request:
instance.user = self.request.user
instance.save()
return instance
class Meta:
model = Books
fields = ('title',
'language', 'status', 'short_desc',
)
You have to get the new book instance and assign the user to it, not to the form try:
if form.is_valid():
book = form.save(commit=False)
book.user = user
book.save()
return redirect('main:books')
See examples here Form's save method.
Try using CreateView for your AddbooksView as:
class AddBooksView(CreateView):
# ...
#specify your form
def form_valid(self, form):
form.save(commit=False)
form.user = self.request.user
form.save()
print("book post sucess")
return redirect('main:books')
#...
For reference follow this link: http://melardev.com/eng/blog/2017/11/04/createview-django-4-examples/
Related
I am trying to give a user the option to change his/her first/last name through a ModelForm. When I press submit, I get hit with the UNIQUE constraint failed: auth_user.username error. Here are my codes:
students/forms.py:
class EditProfileForm(UserChangeForm):
def clean_password(self):
# Overriding the default method because I dont want user to change
# password
pass
class Meta:
model = User
fields = (
'first_name',
'last_name',
)
students/views.py:
User = get_user_model()
def student_profile_view(request, slug):
if request.method == 'GET':
# forms
edit_name_form = EditProfileForm(instance=request.user)
context = {
'edit_name_form': edit_name_form,
}
return render(request, "students/profile.html", context)
class ChangeNameView(SuccessMessageMixin, UpdateView):
template_name = 'students/edit_profile.html'
model = User
form_class = EditProfileForm
success_message = "Your name has been updated"
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
form.instance.student_profile = StudentProfile.objects.get(slug=request.user.student_profile.slug)
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
"""If the form is valid, save the associated model."""
form.instance.username = self.request.user
self.object = form.save(commit=False)
return super().form_valid(form)
def get_success_url(self):
return reverse('students:student_profile_view', kwargs={'slug': self.object.student_profile.slug})
also fyi, User model is foreign key with StudentProfile.
students/models.py:
class StudentProfile(models.Model):
user = models.OneToOneField(User, related_name='student_profile', on_delete=models.CASCADE)
slug = models.SlugField(blank=True, unique=True)
avatar = models.ImageField(upload_to='student_profile/', null=True, blank=True)
description = models.CharField(max_length=120, null=True, blank=True)
objects = models.Manager()
def __str__(self):
return self.user.username
def get_absolute_url(self):
return reverse("students:student_profile_view", kwargs={"slug": self.slug})
I am pretty new to class based view so maybe I'm doing something wrong there?
I assume you do not have the user within the form so you need the form
def get_context_data (self, *args, **kwargs)
ctx = super().get_context_data(*args, **kwargs)
if self.request.method == 'POST':
ctx['form'] = EditProfileForm(instance=self.request.user)
and remove def form_valid()
I'm developing the web site where users can register and edit their information. I would like to use multi form in the single page. I can develop register form using CreateView. But when I use UpdateVIew, I don't know how to add initial value in multi form.
My code(view.py) is below. I can add form data as initial value, but not form2. Please tell me how to add form2 data as initial value.
▪️view.py
class UserUpdate(OnlyYouMixin, generic.UpdateView):
model = Profile
form_class = ProfileForm
second_form_class = ProfileNearStationForm
template_name = 'accounts/user_form.html'
def get_context_data(self, **kwargs):
context = super(UserUpdate, self).get_context_data(**kwargs)
if 'form' not in context:
context['form'] = self.form_class(self.request.GET, instance=self.request.user)
if 'form2' not in context:
context['form2'] = self.second_form_class(self.request.GET, instance=self.request.user)
return context
def get(self, request, *args, **kwargs):
super(UserUpdate, self).get(request, *args, **kwargs)
form = self.form_class
form2 = self.second_form_class
return self.render_to_response(self.get_context_data())
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.form_class(request.POST, instance=self.request.user)
form2 = self.second_form_class(request.POST, instance=self.request.user)
if form.is_valid() and form2.is_valid():
profile = form.save(commit=False)
profile.save()
station = form2.save(commit=False)
station.user = user
station.save()
messages.success(self.request, 'Settings saved successfully')
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(
self.get_context_data(form=form, form2=form2))
def get_success_url(self):
return resolve_url('accounts:user_form', pk=self.kwargs['pk'])
▪️form.py
class ProfileForm(forms.ModelForm):
name = forms.CharField(required=True)
tel_number = forms.CharField(required=True)
class Meta:
model = Profile
fields = ('name', 'tel_number')
class ProfileNearStationForm(forms.ModelForm):
prefecture = forms.ModelChoiceField(queryset=areas_model.Prefecture.objects.all(), required=True)
railway = forms.ModelChoiceField(queryset=areas_model.Railway.objects.none(), required=True)
station = forms.ModelChoiceField(queryset=areas_model.Station.objects.none(), required=True)
memo = forms.CharField(required=False)
class Meta:
model = ProfileNearStation
fields = ('prefecture', 'railway', 'station', 'memo')
▪️model.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
tel_number = models.CharField(max_length=13)
class ProfileNearStation(models.Model):
prefecture = models.ForeignKey(areas_model.Prefecture, on_delete=models.CASCADE)
railway = models.ForeignKey(areas_model.Railway, on_delete=models.CASCADE)
station = models.ForeignKey(areas_model.Station, on_delete=models.CASCADE)
memo = models.CharField(max_length=255, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
I would like to know how I can do that : save an object in my database through Django model with custom field.
This is my modelclass :
class Document(EdqmTable):
language = models.CharField(max_length=2, verbose_name=_('language'), choices=LANGUAGE_CHOICES, null=False)
format = models.CharField(max_length=10, verbose_name=_('format'), choices=FORMAT_CHOICES, null=False)
title = models.CharField(max_length=512, verbose_name=_('document title'), null=False)
publication = models.ForeignKey(Publication, verbose_name=_('publication title'), null=False,
related_name='documents')
class Meta:
verbose_name = _('document')
verbose_name_plural = _('documents')
def save(self, *args, **kwargs):
self.title = f"{self.publication.pub_id}-{self.format.upper()}"
super(Document, self).save(*args, **kwargs)
I have a save method, which let to define my field title with combination between two fields.
title field is hidden in my django form and is set automatically by Django.
But, up to now, it doesn't work because my object is not saved into my database. Something is wrong in my save function ?
EDIT :
I edited my post with forms.py file and my view :
The field document.title is used in my django forms with formset like this :
class PublicationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['category'].empty_label = _('Select a category') # Modify initial empty_label
class Meta:
model = Publication
fields = ['title', 'pub_id', 'category']
class DocumentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DocumentForm, self).__init__(*args, **kwargs)
self.fields['title'].widget = forms.HiddenInput()
for key in self.fields:
self.fields[key].required = True
class Meta:
model = Document
fields = ['publication', 'language', 'format', 'title', 'upload']
DocumentFormSet = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1)
And my view according to this part :
class PublicationCreateView(EdqmCreateView):
""" Create publication with document form through formset """
model = Publication
template_name = 'freepub/publication_form.html'
def get_context_data(self, **kwargs):
context = super(PublicationCreateView, self).get_context_data(**kwargs)
context['document_form'] = DocumentFormSet(self.request.POST or None, self.request.FILES or None)
return context
def form_valid(self, form):
context = self.get_context_data()
document = context['document_form']
if document.is_valid():
self.object = form.save()
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
def get_success_url(self):
return reverse('publication-list-crud')
I have a ManytoMany field and the results of the CheckboxSelectMultiple don't get saved in the db and i don't understand why. It must be really simple but...
Here's the code:
models.py
class Person(models.Model):
last_name = models.CharField(max_length = 50)
first_name = models.CharField(max_length = 50)
def __str__(self):
return self.last_name +" "+self.first_name
class Event(models.Model):
owner = models.ForeignKey(User, on_delete=models.SET_NULL, null = True)
person = models.ManyToManyField(Person)
forms.py
class EventForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.get('user',None)
#self.user = kwargs.pop('user',None)
super(EventForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'POST'
#self.helper.form_action = reverse_lazy('simpleuser')
self.helper.add_input(Submit('submit', 'Submit', css_class='btn-success'))
class Meta:
model = Event
fields = ['person']
widgets ={
'person': forms.CheckboxSelectMultiple,
}
views.py
def uploadevent(request):
if request.method == "POST":
form =EventForm(request.POST)
if form.is_valid():
event = form.save(commit=False)
event.owner = request.user
event.save()
else:
form = EventForm()
return render...
See the response on this stackoverflow question: Saving Many To Many data via a modelform in Django
Quoting the OP:
When using commit=False, you have to call save_m2m()
m2m relationships require the parent object to be saved first, which
you are not doing by using commit=False
Just add this line below event.save()
if form.is_valid():
event = form.save(commit=False)
event.owner = request.user
event.save()
form.save_m2m()
Ref: https://docs.djangoproject.com/en/2.0/topics/forms/modelforms/#the-save-method
To save manytomany you should call save_m2m() when using commit=False (check details here):
if form.is_valid():
event = form.save(commit=False)
event.owner = request.user
event.save()
form.save_m2m()
I have a model Course that has the following attr:
class Course(models.Model):
user= models.ForeignKey(
User,
on_delete=models.CASCADE,
)
# email= models.EmailField(default=user.email)
courseName= models.CharField(max_length=20, blank=False)
class Meta:
unique_together= ('user','courseName',)
def __str__(self):
return self.courseName
I have created a form where I want the user to enter just the courseName and after they POST it, I will add the requested user in the model as well.
This is my form which is getting passed on to the template via my ListView
forms.py
class CourseForm(forms.ModelForm):
class Meta:
model= Course
fields = ['courseName']
**Here is my views.py where I am struggling with **
class CoursesListView(ListView, FormMixin):
model = Course
form_class = CourseForm
template_name = "userApp/courseList.html"
def get_queryset(self):
return Course.objects.filter(user__exact=self.request.user)
def get_context_data(self,*args,**kwargs):
context= super(CoursesListView,self).get_context_data(*args, **kwargs)
context['courseForm'] = self.form_class
return context
def post(self,request, *args, **kwargs):
form = self.form_class(request.POST)
user = User.objects.get(username__exact=self.request.user)
**I want to add the user to my model.user field here**
return self.get(redirect, *args, **kwargs)
def get(self,request, *args, **kwargs):
self.object=None
self.form = self.get_form(self.form_class)
return ListView.get(self, request, *args, **kwargs)
So basically my question is how can I add the user in my model before calling form.is_valid().
something like this?
def post(self,request, *args, **kwargs):
form_data = copy.copy(request.POST)
form_data['user'] = User.objects.get(username__exact=self.request.user).pk
form = self.form_class(form_data)
# form handling follows
return self.get(redirect, *args, **kwargs)
This Answer was suggested by a user who deleted this answer. Didnt get his user id but whoever you were thanks a lot for the help!!!
Just use form.save(commit=False) and then make the necessary changes.
def post(self,request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
user = User.objects.get(username__exact=self.request.user)
instance = form.save(commit=False)
instance.user = user
instance.save()