Answer the question
In order to leave comments, you need to log in
How to search for duplicates in the DB through the form before adding?
How to check for two fields 'tin' and 'city'? That is, if, when adding a company, there is a 100% match in these two fields, the user will receive a message that the client is booked.
For example:
tin = 1234567890
city = Moscow
tin = 1234567890
city = Sarov
This addition is possible, the ban on booking only if both fields match.
from django.db import models
import datetime
from django.utils import timezone
import getpass
from django.contrib.auth.models import User
# Create your models here.
class factory(models.Model):
city = models.CharField('Город', max_length=30)
name = models.CharField('Наименование организации', max_length=50)
tin = models.CharField('ИНН', max_length=12, unique=True)
equipment = models.CharField('Оборудование', max_length=50)
dealer = models.ForeignKey(User, verbose_name='Дилер', on_delete=models.CASCADE, max_length=50)
createdate = models.DateField(verbose_name='Дата бронирования', default=(timezone.localtime(timezone.now()).strftime('%Y-%m-%d')), max_length=50)
def __str__(self):
return self.name
class Meta:
verbose_name = 'Организация'
verbose_name_plural = 'Организации'
from .models import factory
from django.forms import ModelForm, TextInput
class FactoryForm(ModelForm):
class Meta:
model = factory
fields = ['city', 'name', 'tin', 'equipment']
widgets = {
"city": TextInput(attrs={
'class': 'search-city',
'type': 'text',
'list': 'city',
'placeholder': 'Город (Например: Москва)'
}),
"name": TextInput(attrs={
'class': 'search-llcname',
'type': 'text',
'placeholder': 'Наименование организации (Например: ООО "ЭИР-ПАРТ")'
}),
"tin": TextInput(attrs={
'class': 'search-tin',
'type': 'text',
'maxlength': '12',
'minlength': '10',
'placeholder': 'ИНН'
}),
"equipment": TextInput(attrs={
'class': 'search-equipment',
'type': 'text',
'placeholder': 'Поставляемое оборудование'
}),
}
from django.shortcuts import render, redirect
from .models import factory
from .forms import FactoryForm
# Create your views here.
def index(request):
return render(request, 'main/index.html')
def factory(request):
error = ''
if request.method == 'POST':
form = FactoryForm(request.POST)
if form.is_valid():
form.instance.dealer = request.user
form.save()
else:
error = 'Ошибка обработки данных полей'
form = FactoryForm()
data = {
'form': form,
'error': error
}
return render(request, 'main/factory.html', data)
def myfactory(request):
return render(request, 'main/myfactory.html')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question