I am new to django and I created this "apply now form" exclusively for tutors that when they submit the form it will appear to the admin site, and I will manually check it if they are a valid tutor. And if they are a valid tutor, I will check the is_validated booleanfield in the admin site to the corresponding tutor that sent the form, so that he/she will have access to other things in the site. But I am having this problem that when you submit the form this comes up..
NOT NULL constraint failed: account_tutorvalidator.user_id
I have search for some solutions and also read similar questions here but I still couldn't understand what to do.. could someone help me out with this?
here is my models.py
class User(AbstractUser):
is_student = models.BooleanField(default=False)
is_tutor = models.BooleanField(default=False)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
phone_number = models.CharField(max_length=11, blank=False, null=True)
current_address = models.CharField(max_length=100, null=True)
image = models.ImageField(default='default-pic.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.first_name} {self.last_name}'
class TutorProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True,
related_name='tutor_profile')
bio = models.CharField(max_length=255, blank=True)
is_validated = models.BooleanField(default=False)
def __str__(self):
return f"{self.user.first_name} {self.user.last_name}'s Profile"
class TutorValidator(models.Model):
user = models.ForeignKey(TutorProfile, on_delete=models.CASCADE)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
dbs = models.ImageField(upload_to='dbs_pics')
driving_license = models.ImageField(upload_to='drivers_license_pics', null=True, blank=True)
national_id = models.ImageField(upload_to='national_id_pics', null=True, blank=True)
def __str__(self):
return f"{self.first_name}'s application form"
my forms.py
class TutorValidationForm(forms.ModelForm):
class Meta:
model = TutorValidator
fields = ['first_name', 'last_name', 'driving_license', 'national_id']
labels = {
'national_id': _('National ID')
}
my views.py
class TutorValidatorView(LoginRequiredMixin, FormView):
template_name = 'account/tutor_validator.html'
form_class = TutorValidationForm
success_url = '/'
The error is because TutorValidator requires that you set the user profile foreign key which your form currently does not support, so you need a way to set this to the object you are creating, and use the current logged in user (the one who is submitting the form).
You can do this by overriding form_valid. Try with:
class TutorValidatorView(LoginRequiredMixin, FormView):
...
def form_valid(self, form):
tutor_validator = form.save(commit=False)
tutor_validator.user = self.request.user.tutor_profile
tutor_validator.save()
return HttpResponseRedirect(self.get_success_url())
Note that the current user needs to already have an existing TutorProfile. Otherwise you need to create that first to connect it to TutorValidator
Related
I'm trying to do a system where an user gains points if he asks a question but the points field isn't increasing when a user does that.
my model:
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='post')
category = models.ForeignKey(Category, on_delete=models.PROTECT)
type = models.CharField(max_length=30, choices=TYPE, default='Question')
title = models.CharField(max_length=100, unique=True)
content = models.TextField()
views = models.IntegerField(default=0)
votes = models.ManyToManyField(User, blank=True, related_name='vote')
featured = models.BooleanField(default=False)
date_posted = models.DateTimeField(default=timezone.now)
my view:
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
success_url = '/'
fields = ['title', 'content', 'category']
def form_valid(self, form):
form.instance.author = self.request.user
form.instance.author.points + 15
return super().form_valid(form)
When I go to the current user in the admin page the value doesn't change.
You should add more clarity to your code, but as I can assume and as Thierno said you are not accessing the object and not saving it afterwards.
What you need to do is once you make your post request, --and since you need access to the user--, save your post and then do something like:
post_user = self.request.user
post_user.points +=15
post_user.save()
Another question from me tonight and I hope I can explain it adequately:
I got three classes in my "models.py":
class Customer(models.Model):
full_name = models.CharField(max_length=100, null=True, unique=True)
short_name = models.CharField(max_length=8, null=True, unique=True)
class Project(models.Model):
customer = models.ForeignKey(Customer, null=False, on_delete=models.CASCADE)
name = models.CharField(max_length=255, null=True, unique=True)
...
class Entry(models.Model):
user = models.ForeignKey(User, null=True, blank=False, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer, null=True, blank=False, on_delete=models.CASCADE)
project = models.ForeignKey(Project, null=True, blank=False, on_delete=models.CASCADE)
date = models.DateField()
shortText = models.CharField(max_length=100, null=False, blank=False)
...
Note: One Customer can have multiple Projects.
On one of my sites there's a table with buttons beside each "Customer". The plan is, that it should lead me to another page, were the user can write and save his "Entry". Right now, the PrimaryKey inside the Button/Link contains the ID of the "Customer".
My question is: is it possible to limit the choices of the "Project" (inside a Drop-Down-Menu) to the "Customer" that has been clicked on? And is creating a ModelForm the right thing to do?
Thanks to all of you and a good night!
Well, don't know if this is the right way, but I found a solution for my problem:
Wrote a "forms.ModelForm" for my view-function...
class EntryForm(ModelForm):
class Meta:
model = Entry
fields = '__all__'
def __init__(self, *args, pk, **kwargs):
super().__init__(*args, **kwargs)
self.fields['project'].queryset = Project.objects.filter(customer_id=pk)
Insert my ModelForm into the view-function...
def WriteEntry(request, pk):
form = EntryForm(pk=pk)
if request.method =='POST':
form = EntryForm(request.POST, pk)
if form.is_valid():
form.save()
...
context = {'form': form}
return render(request, '...html', context)
So I know there are a few similar questions but none of the solutions worked for me. I've tried save(commit=false) and save_m2m as well as a bunch of other stuff but i get the error
NOT NULL constraint failed: home_services.managers_id
Anyways here's my code:
views.py
def service(request):
if request.user.is_authenticated:
if request.method == 'POST': #Create Service
form = CreateServiceForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/service') #Later change this to redirect to server page
else:
form = CreateServiceForm()
args = {'user': request.user, 'form': form}
return render(request, 'service.html', args)
else:
return HttpResponseRedirect('/feed')
models.py
class Services(models.Model):
name = models.CharField(max_length=100, default='')
description = models.CharField(max_length=500, default='')
owner = models.OneToOneField(User, on_delete=models.CASCADE)
managers = models.ForeignKey(User, related_name="managers", on_delete=models.CASCADE)
members = models.ManyToManyField(User, related_name="members")
def __str__(self):
return str(self.name) + ": id" + str(self.id)
forms.py
class CreateServiceForm(forms.ModelForm):
owner = forms.ModelChoiceField(queryset=User.objects.all())
members = forms.ModelMultipleChoiceField(queryset=User.objects.all())
class Meta:
model = Services
fields = [
'name',
'description',
'owner',
'members',
]
I want the user to be able to create a service and select 1 or more members that is in the default django User model.
I want the user to be able to create a service and select 1 or more members that is in the default django User model.
You are not providing managers, which is not allowed as per the current configuration.
So, change:
managers = models.ForeignKey(
User,
related_name="managers",
on_delete=models.CASCADE,
)
To:
managers = models.ForeignKey(
User,
related_name="managers",
on_delete=models.CASCADE,
blank=True,
null=True,
)
You might want to read more about blank and null.
I want to have a form which only offers the user to post a question for a project he is participating in.
models.py:
class Project(models.Model):
project_name = models.CharField(max_length=255, unique=True, blank=False)
def __str__(self):
return str(self.project_name)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
project = models.ManyToManyField(Project)
def __str__(self):
return str(self.user)
class Question(models.Model):
title = models.CharField(max_length=255, blank=False)
content = tinymce_models.HTMLField(blank=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
...
def __str__(self):
return str(self.title)
class QuestionForm(ModelForm):
class Meta:
model = Question
fields = ['title', 'content', 'project']
in views.py:
form = QuestionForm()
form.fields["project"].queryset = Project.objects.filter(project_name__in=request.user.profile.project.all())
But somehow the result of the query always stays empty.
Does somebody maybe have an idea what I am missing?
Your query is over complicated. You should just use the user's projects directly:
form.fields["project"].queryset = request.user.profile.project.all())
I have custom user model by extending AbstractUser class. I want to make form to change user's fullname and website field.
My user:
class Hacker(AbstractUser):
name = models.CharField(max_length=255)
team = models.ForeignKey(Team, on_delete=models.CASCADE, blank=True, null=True)
description = models.CharField(max_length=255, blank=True, null=True)
website = models.URLField(max_length=200, blank=True, null=True)
def __str__(self):
if self.name:
return self.name
else:
return self.username
And forms.py:
class ProfileForm(forms.ModelForm):
"""
Edit profile form
"""
name = forms.CharField(label=_("Name"),
widget=forms.TextInput(attrs={'placeholder': _('Name')}))
description = forms.CharField(label=_("Description,Position"), required=False,
widget=forms.TextInput(attrs={'placeholder': _('Description, Position')}))
website = forms.URLField(label=_("Website"), required=False,
widget=forms.TextInput(attrs={'placeholder': _('Website URL')}))
class Meta:
model = get_user_model()
fields = ['name', 'description', 'website']
In page I use {{ field }} to add inputs and in POST part of views:
form = ProfileForm(request.POST)
if form.is_valid():
form.save(commit=True)
...
But form.save gives UNIQUE constraint failed: common_hacker.username error. What can be problem here?
Adding instance=request.user when creating form fixed problem:
form = ProfileForm(request.POST, instance=request.user)