How check UniqueConstraint in Django with CreateView and custom forms if field set in view? - django-models

I do:
I define UniqueConstraint (also try with 'unique_together') in model:
class Project(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(
fields=['company', 'name'], name="unique_project_name_in_company"
)
]
name = models.CharField(blank=False, max_length=256)
company = models.ForeignKey(
Company,
on_delete=models.CASCADE
)
I set company in form_valid in view (I think it's reason of my problem):
class ProjectCreateView(LoginRequiredMixin, generic.CreateView):
model = Project
form_class = ProjectForm
def form_valid(self, form):
form.instance.company = self.request.user.company
return super().form_valid(form)
I try define message for 'unique_project_name_in_company' in form:
class ProjectForm(forms.ModelForm):
model = Project
class Meta:
model = Project
fields = ['name']
error_messages = {
NON_FIELD_ERRORS: {
'unique_project_name_in_company': "Name isn't unique!",
}
}
Unexpected behavior
If I submit form with non-unique pair (inputed non-unique name) I want get my custom error_message but I get:
500 IntegrityError UNIQUE constraint failed: company_id, name

Related

How can I create multiple related objects inside a view with cleaned data from two Django forms on the same page

I have 4 related Django Models ModifiedUser and Profile defined as follows . I am trying to create the person responsible for the activity,through a person model form. Through a person profile model form I am trying to add their profile details which includes the related person,country etc. Using these details I want to create a second person, called related user. Then I want to assign the two people to the activities via their Roles.
Models
'''
Class ModifiedUser:
email = models.EmailField(
max_length=255,blank=True,null=True,unique=True)
...
Class Profile:
modified_user = models.ForeignKey(ModifiedUser)
related_user = models.EmailField(
max_length=255,blank=True,null=True)
...
class Activity:
person = models.ManyToManyField(Profile,related_query_name='actor',related_name=' person', through='RoleAssignment')
class RoleAssignment:
person = models.ForeignKey(Profiles,on_delete=CASCADE,related_name="role")
feedback = models.ForeignKey(Activity,on_delete=CASCADE)
...
'''
Forms:
'''
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = '__all__'
class ModifiedUserForm(forms.ModelForm):
class Meta:
model = ModifiedUser
fields = '__all__'
'''
View
'''
from profiles.models import Profile, ModifiedUser,Activity
from .forms import ResponsibleForm, ModifiedUser
def add_activity_owner(request):
activity = Activity.objects.create(initiatedby=currentuser,updatedby=currentuser)
if request.method == 'POST':
responsibleform = ResponsibleForm(request.POST, request.FILES)
profileform = ProfileForm(request.POST, request.FILES)
if all([receiverform.is_valid(), detailform.is_valid()]):
owner_firstname =responsibleform .cleaned_data['Phone']
owner_lastname = responsibleform .cleaned_data['Address']
owner_email = responsibleform .cleaned_data['email']
relatedperson_email= profileform.cleaned_data['related_user'],
country = profileform.cleaned_data['country'],
gender = profileform.cleaned_data['gender'],
relatedperson,person_created = ModifiedUser.objects.get_or_create(email=relatedperson_email,FirstName=NULL,LastName=NULL)
owner,owner_created=Profiles.objects.get_or_create(email=owner_email,FirstName=owner_firstname,Lastame=owner_lastname)
owner_profile ,owner_created= Profiles.objects.get_or_create(user=owner,
related_user= relatedperson_email,
country = country,
gender = gender,
owner_role = RoleAssignment.objects.create(activity=activity,person=owner,role=role['owner'])
related_person_role = RoleAssignment.objects.create(activity=activity,person=relatedperson,role=role['actor'])
context['relateduser']=relateduser
context['owner']=owner
context['owner_role']=owner_role
return redirect(selectroles,activity)
else:
responsibleform = GiverForm()
profileform = UserProfileUpdateForm()
profileform':profileform})
return render(request,'addowner.html',context)
'''

How to make required filling in a form field with a many-to-many relationship in django admin

I have a Recipes model with an ingredients field with a many-to-many relationship. I have an intermediate Ingredient amount model associated with the ingredients field via through. I want to prohibit the creation of a recipe in django admin without ingredients. I'm trying to solve this problem by creating my own class inherited from BaseInlineFormSet and redefining the clean() method. It doesn't work out yet, I need help, what am I doing wrong?
admin.py
class IngredientsAmountInlineFormset(BaseInlineFormSet):
def clean_ingredients(self):
if len(self.cleaned_data['ingredients']) < 1:
return 'Укажите хотя бы один ингредиент в рецепте'
return self.cleaned_data['ingredients']
class IngredientsAmountInline(admin.TabularInline):
model = IngredientsAmount
formset = IngredientsAmountInlineFormset
extra = 0
#admin.register(Recipes)
class RecipesAdmin(admin.ModelAdmin):
list_display = ('name', 'author', 'favorites_count',)
list_filter = ('name', 'author', 'tags',)
search_fields = ('name', 'author', 'tags',)
empty_value_display = '-пусто-'
inlines = (IngredientsAmountInline,)
models.py
class Ingredients(models.Model):
name = models.CharField(max_length=200, unique=True)
measurement_unit = models.CharField(max_length=200)
...
class Recipes(models.Model):
...
ingredients = models.ManyToManyField(Ingredients,
through='IngredientsAmount',
related_name='recipes',
verbose_name='Ингредиенты',
)
...
class IngredientsAmount(models.Model):
ingredients = models.ForeignKey(Ingredients,
on_delete=models.CASCADE,
related_name='amount',
verbose_name='Ингредиенты',
)
recipes = models.ForeignKey(Recipes,
on_delete=models.CASCADE,
related_name='amount',
verbose_name='Рецепты', )
amount = models.IntegerField()
obiviously clean should raise ValidationException:
class IngredientsAmountInlineFormset(BaseInlineFormSet):
def clean_ingredients(self):
value = self.cleaned_data['ingredients']
if value:
return value
raise ValidationError(_('Укажите хотя бы один ингредиент в рецепте'))
by the way:
Try don't use the direct text message:
'Укажите хотя бы один ингредиент в рецепте'
Wrap your text in gettext/gettext_lazy, like this:
from django.utils.translation import gettext_lazy as _
_('Укажите хотя бы один ингредиент в рецепте')
It help you in future collect those messages together, and translate it if you need.

How to add check constraint validator in serializer in django rest framework?

I have a model defined as follows :
class Order(models.Model):
qty = models.FloatField()
pending_qty = models.FloatField(default=0)
class Meta:
constraints = [
models.CheckConstraint(check=models.Q(
pending_qty__gte=0), name='pending_qty__gte_0')
]
The serializer for this models is as follows:
class NewOrderSz(serializers.ModelSerializer):
class Meta:
model = Order
fields = '__all__'
I want to add a validator in the serializer to validate for the 'pending_qty__gte_0' CheckConstraint in the model. How can I do that ?
View for creating an entity is :
class NewOrder(generics.CreateAPIView):
queryset = models.Order.objects.none()
serializer_class = serializers.NewOrderSz
Just define your pending_qty with FloatField with a min_value constraint
and you should be fine
class NewOrderSz(serializers.ModelSerializer):
pending_qty = serializers.FloatField(min_value=0)
class Meta:
model = Order
fields = '__all__'
class NewOrderSz(serializers.ModelSerializer):
class Meta:
model = Order
fields = '__all__'
def validate_pending_qty(self, value):
if value < 0:
raise serializers.ValidationError("Pending Qty Less Than Zero!")
return value
https://www.django-rest-framework.org/api-guide/serializers/#field-level-validation

Django Unable to set widget in form class

I want to create a form that hides a field. This works:
form_class = modelform_factory( Model, widgets = { 'field1' : forms.HiddenInput() }, exclude = () )
form = form_class() # field1 is hidden
Why doesn't it work when I create the form_class and then set the widgets through its meta class?
form_class = modelform_factory( Model, exclude=() )
form_class._meta.widgets = { 'field1' : forms.HiddenInput() }
form = form_class() # field1 is not hidden in form

How to create model objects via ModelForm ForeignKey?

I have a model for Classroom and Student as shown below
class Classroom(models.Model):
COURSE_NAME = (
('MA8', 'Math 8'),
('SC10', 'Science 10'),
('PH11', 'Physics 11'),
('PH12', 'Physics 12'),
)
BLOCK_NUMBER = (
('11', 'Block 1-1'),
('12', 'Block 1-2'),
('13', 'Block 1-3'),
('14', 'Block 1-4'),
('21', 'Block 2-1'),
('22', 'Block 2-2'),
('23', 'Block 2-3'),
('24', 'Block 2-4'),
)
class_list = models.TextField()
course_name = models.CharField(max_length=20, choices=COURSE_NAME)
course_block = models.CharField(max_length=10, choices=BLOCK_NUMBER)
class Student(models.Model):
classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE)
nickname = models.CharField(default='JohnS', max_length=31)
attend = models.BooleanField(default=True)
I created a form for Classroom.class_list and TextField is where the user copy/pastes a list of names. I want to then parse the class_list and save each individual name as nickname. I first tried the following but this doesn't seem to save the Student objects.
forms.py
class ClassroomForm(ModelForm):
class Meta:
model = Classroom
fields = ['course_name', 'course_block','class_list']
views.py
class ClassroomCreateView(CreateView):
model = Classroom
form_class = ClassroomForm
def form_valid(self, form):
classroom = form.save(commit=False)
s = Student()
for line in classroom.class_list:
s.nickname = line
s.save()
classroom.save()
return super(ClassroomCreateView, self).form_valid(form)
def get_success_url(self):
return reverse('classroom:submitted')
I also tried creating StudentForm which allows a user to choose course_name and course_block (which corresponds to a particular class_list). The form or view would then create the individual Student objects and display them. I read about ModelChoiceField but I can't figure out how to implement this.
How and where do I (auto) create Students objects from a ForeignKey field?
I solved my question with the help of this answer. Here is the modified code I used for models.py. My view is just a standard CreateView from the ModelForm.
class Classroom(models.Model):
... dictionary stuff ..
class_list = models.TextField()
course_name = models.CharField(max_length=20, choices=COURSE_NAME)
course_block = models.CharField(max_length=10, choices=BLOCK_NUMBER)
group_size = models.IntegerField(default=3)
def __str__(self):
return self.get_course_block_display()
def save(self, *args, **kwargs):
super(Classroom, self).save(*args, **kwargs)
# overrides the default save function to parse the class list
studentList = []
studentList = self.class_list.split('\n')
for line in studentList:
line = line.strip('\r')
s = Student.objects.create(nickname = line, classroom = self)

Resources