I'm using Django's CreateView in order to fill a form and I want some of the fields to be automatically filled in, looking for ides how I could that. the fields that I want to be filled in automatically are company, recruiter and date
this is what the views file looks like:
class CreateNewJobForm(CreateView):
model = Job
fields = (
'title', 'company', 'recruiter', 'job_type', 'work_from', 'description', 'city', 'address', 'title_keywords',
'date_created')
template_name = 'create_new_job_form.html'
success_url = '/job_created_successfully'
def form_valid(self, form):
form.instance.recruiter = self.get_name()
return super(CreateNewJobForm, self).form_valid(form)
and this is what the models file looks like:
class Recruiter(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)
name = models.CharField(max_length=255)
company = models.ForeignKey(Company, on_delete=models.RESTRICT, related_name='recruiters')
email = models.EmailField(max_length=255)
phone_number = models.CharField(max_length=15, blank=True)
Something like that should work just fine :
def form_valid(self, form):
form.instance.user = self.request.user # assuming you want the current login user to be set to the user
return super(CreateNewJobForm, self).form_valid(form)
It is just an example but in short you can access attributes of your model by accessing the instance of your form like that form.instance.yourfield
Automatically assign the value
We can assign a value without showing this in the form. In that case, you remove the company, recruiter, and date_created fields from the fields, and fill these in in the form_valid method:
from django.contrib.auth.mixins import LoginRequiredMixin
class CreateNewJobForm(LoginRequiredMixin, CreateView):
model = Job
fields = ('title', 'job_type', 'work_from', 'description', 'city', 'address', 'title_keywords')
template_name = 'create_new_job_form.html'
success_url = '/job_created_successfully'
def form_valid(self, form):
recruiter = form.instance.recruiter = self.request.user.recruiter
form.instance.company_id = recruiter.company_id
return super().form_valid(form)
for the date_created, you can work with the auto_now_add=True parameter [Django-doc] of the DateTimeField:
class Job(models.Model):
# …
date_created = models.DateTimeField(auto_now_add=True)
Provide an initial value
We can also provide an initial value for the form by overriding the .get_initial() method [Django-doc]:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.timezone import now
class CreateNewJobForm(LoginRequiredMixin, CreateView):
model = Job
fields = ('title', 'job_type', 'work_from', 'description', 'city', 'address', 'title_keywords', 'company', 'recruiter', 'date_created')
template_name = 'create_new_job_form.html'
success_url = '/job_created_successfully'
def get_initial(self):
recruiter = self.request.user.recruiter
return {
'recruiter': recruiter,
'company': recruiter.company,
'date_created': now()
}
Related
I've set the default post author to be null, and used the form_valid function to override the post author and assign to it the current logged in user.
the form_valid() function is taken from the official django docs but for some reason its doesnt do anything.
my django versions are:
django-rest-framework = 3.12.2.
django = 3.1.4
models.py
class Recipe(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
author = models.ForeignKey(get_user_model() , on_delete=models.CASCADE, null=True) #
title = models.CharField(max_length=150)
description = models.TextField(blank=True)
serializers.py
class RecipeCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Recipe
fields = ('title', 'description')
views.py
class RecipeCreate(CreateAPIView):
permission_classes = (permissions.IsAuthenticated, )
queryset = Recipe.objects.all()
serializer_class = RecipeCreateSerializer
def form_valid(self, form):
form.instance.author = self.request.user
return super(RecipeCreate, self).form_valid(form)
hopefully someone out here will know how to fix this.
thanks in advance
I think you are mixing up Django's class based views and Django rest framework views. In the docs, it is stated that if you want to use request.user in your serializer, you must use the perform_create method. So first, you would have to add the author field to your RecipeCreateSerializer in serializers.py.
class RecipeCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Recipe
fields = ('title', 'description', 'author') # Add author now
and your RecipeCreate View would now have the perform_create method instead of form_valid:
class RecipeCreate(CreateAPIView):
permission_classes = (permissions.IsAuthenticated, )
queryset = Recipe.objects.all()
serializer_class = RecipeCreateSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
I am trying to create a simple API to get a user register.
I am using the default User table for authentication purpose, created another table called "phone" with one to one relation with User.
I am trying to add "phone" field just above the password. (I hope the image attached is visible).
**
Serializer.py
class UserRegisterSerializer(serializers.ModelSerializer):
class Meta:
model = UserDetailsModel
fields = ('phone', 'user')
class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(max_length=68, min_length=6, write_only=True)
class Meta:
model = User
fields = ('username','first_name', 'last_name','email','password')
read_only_fields = ('id',)
**
models.py<<
**
class UserDetailsModel(models.Model):
phone = models.IntegerField()
balance = models.DecimalField(max_digits=10, decimal_places=2, default=0)
user = models.OneToOneField(get_user_model(),primary_key='email' , on_delete=models.CASCADE)
def __str__(self):
return str(self.user)
**
views.py
**
class RegisterView(generics.GenericAPIView):
serializer_class = RegisterSerializer
def post(self, request):
user = request.data
serializer = self.serializer_class(data=user)
serializer.is_valid(raise_exception=True)
serializer.save()
user_data = serializer.data
return Response(user_data,status=status.HTTP_201_CREATED)
class DetailsRegisterView(generics.GenericAPIView):
serializer_class = UserRegisterSerializer
def post(self, request):
user = request.data
serializer = self.serializer_class(data=user)
serializer.is_valid(raise_exception=True)
serializer.save()
user_data = serializer.data
return Response(user_data,status=status.HTTP_201_CREATED)
**
urls
**
urlpatterns = [
path('',RegisterView.as_view()),
path('details', DetailsRegisterView.as_view())
]
**
You probably can use source in a serializer with a FK
class RegisterSerializer(...)
...
phone = serializers.CharField(..., source='userdetails.phone')
see also : the doc
I have some doubt in create case, in a update case this code work fine.
see also : How to serialize a relation OneToOne in Django with Rest Framework?
and an other way to resolve your issue : nested serializer
Updated code:
serializers>
from django.contrib.auth.models import User
from django.http import JsonResponse
from rest_framework import serializers
from .models import UserDetailsModel
class RegisterSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username','first_name', 'last_name','email','password')
read_only_fields = ('id',)
class UserRegisterSerializer(serializers.ModelSerializer):
user = RegisterSerializer(required=True)
class Meta:
model = UserDetailsModel
fields = ('phone','user')
def create(self, validated_data):
user_data = validated_data.pop('user')
user = RegisterSerializer.create(RegisterSerializer(), validated_data=user_data)
data, created = UserDetailsModel.objects.update_or_create(user=user,
phone=validated_data.pop('phone'))
return data
class DetailView(serializers.ModelSerializer):
user = RegisterSerializer(read_only=True)
class Meta:
model = UserDetailsModel
fields = ('user','phone')
Remaining code stays the same.
I've created a "Account" class so i can get more columns on user information:
class Account(AbstractUser):
company_name = models.CharField(max_length=50)
company_department = models.CharField(max_length=50)
company_employees_quantity = models.IntegerField(default=1)
def __str__(self):
return self.username
I've also created a form with this data so i can receive the information on my view.
class AccountCreationForm(UserCreationForm):
company_name = forms.CharField(max_length=50)
company_department = forms.CharField(max_length=50)
company_employees_quantity = forms.IntegerField()
class Meta:
model = Account
fields = ('username', 'email')
The problem is when the client send the data though the form, i receive all the fields but only the "core" user information is inserted on the database.
class SignUpView(CreateView):
form_class = AccountCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
If o add a method form_valid on on view o can save the data field by field like this:
class SignUpView(CreateView):
form_class = AccountCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
def form_valid(self, form):
if form.is_valid():
account = form.save()
account.company_name = form.cleaned_data["company_name"]
account.company_department = form.cleaned_data["company_department"]
account.company_employees_quantity = form.cleaned_data["company_employees_quantity"]
account.save()
return redirect(self.success_url)
return super().form_invalid(form)
But this look weird to me? the view/form/model shouldn't save my "custom" fields automatically along with the "core" user info? How do i do that?
You don't need to define 'company_name', 'company_department' and 'company_employees_quantity' again, you can add them in the fields attribute.
Try the following:
class AccountCreationForm(UserCreationForm):
class Meta:
model = Account
fields = (
'username',
'email',
'company_name',
'company_department',
'company_employees_quantity',
)
I want to save a sent json data to db by django-rest-framework.
the problem is, not saving the relation and returns error.
The bellow snippet is my models:
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile', on_delete=models.CASCADE)
name = models.CharField(max_length=30)
family = models.CharField(max_length=50)
class Klass(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=500)
teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE)
I use below serializer for serializing/deserializing the Klass model.
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('pk', 'name', 'family')
class KlassSerializer(serializers.ModelSerializer):
teacher = ProfileSerializer()
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
now when I prepare a JSON object and send it to the view, it returns error. the below is the view class:
class KlassView(APIView):
"""for SELECT, INSERT Queries"""
def get(self, request, pk):
# somthing
#csrf_exempt
def post(self,request, pk=None):
"""For Creating A Class"""
serializer = KlassSerializer(data=request.data)
if serializer.is_valid():
teacher = ProfileSerializer(request.data['teacher']['pk'])
serializer.teacher = teacher.data
serializer.save()
return Response({'data': serializer.data})
else:
return Response({'data': serializer.errors})
and the error is:
The .create() method does not support writable nested fields by default.
Write an explicit .create() method for serializer mainp.serializers.KlassSerializer, or set read_only=True on nested serializer fields.
How can I save relation in KlassSerializer in order to save to db?
At first change your serializer like below:
class KlassSerializer(serializers.ModelSerializer):
# teacher = ProfileSerializer() # No need to this!
class Meta:
model = Klass
# fields = ('id', 'title', 'description', 'teacher')
fields = ('id', 'title', 'description') # Omit teacher
Then get profile from requested user and pass it to your serializer:
def post(self,request, pk=None):
"""For Creating A Class"""
serializer = KlassSerializer(data=request.data)
if serializer.is_valid():
teacher = ProfileSerializer(request.data['teacher']['pk'])
serializer.teacher = teacher.data
serializer.save(teacher=request.user.profile) # Retrieve teacher and stroe
return Response({'data': serializer.data})
else:
return Response({'data': serializer.errors})
Just override the create method of ModelSerializer in KlassSerializer.
class KlassSerializer(serializers.ModelSerializer):
teacher = ProfileSerializer()
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
def create(self, validated_data):
profile = Profile.objects.filter(pk=validated_data['teacher']['pk'])
if profile:
k = Klass()
k.teacher = profile
...
models.py:
from django.db import models
from django.contrib.auth.models import User as BaseUser
CHOICE_GENDER = ((1, 'Male'), (2, 'Female'))
class Location(models.Model):
city = models.CharField(max_length=75)
country = models.CharField(max_length=25)
def __str__(self):
return ', '.join([self.city, self.state])
class Users(BaseUser):
user = models.OneToOneField(BaseUser, on_delete=models.CASCADE)
gender = models.IntegerField(choices=CHOICE_GENDER)
birth = models.DateField()
location = models.ForeignKey(Location)
class Meta:
ordering = ('user',)
forms.py:
from django.contrib.auth.forms import UserCreationForm
from django import forms
from .models import Users, Location
class LocationForm(forms.ModelForm):
class Meta:
model = Location
fields = '__all__'
class RegistrationForm(UserCreationForm):
class Meta:
model = Users
fields = ('username', 'email', 'first_name', 'last_name', 'gender', 'birth', 'location')
views.py:
def signup(request):
if request.method == "POST":
reg_form = forms.RegistrationForm(request.POST)
loc_form = forms.LocationForm(request.POST)
if loc_form.is_valid():
reg_form.location = loc_form.save()
if reg_form.is_valid():
reg_form.save()
return redirect('./')
reg_form = forms.RegistrationForm()
loc_form = forms.LocationForm()
return render(request, 'signup.html', {'loc_form': loc_form, 'reg_form': reg_form})
I can't manage to make this work, it gives location - This field is required error. I've tried every combination in views.py, and it never passed the reg_form.is_valid() command due to that reason. Can anybody help me in this? Thanks in advance!
SOLVED: views.py new, working code:
def signup(request):
if request.method == "POST":
reg_form = forms.RegistrationForm(request.POST)
loc_form = forms.LocationForm(request.POST)
if reg_form.is_valid():
reg = reg_form.save(commit=False)
if loc_form.is_valid():
reg.location = loc_form.save()
reg.save()
return redirect('./')
reg_form = forms.RegistrationForm()
loc_form = forms.LocationForm()
return render(request, 'signup.html', {'loc_form': loc_form, 'reg_form': reg_form})
Removing location from RegistrationForm fields tuple should stop the behavior.
Since you are using a separate form for Location, you shouldn't populate location field using RegistrationForm.