Answer the question
In order to leave comments, you need to log in
How to query multiple nested ForiengKey fields?
The database stores a list of form templates.
A form template is a structure that is defined by a unique set of fields, indicating their types.
Form template example:
{
"name": "Form template name",
"field_name_1": "email",
"field_name_2": "phone" }
A total of four field data types must be supported:
email
phone
date
text
All types except text must support validation . The phone is transmitted in the standard format +7 xxx xxx xx xx, the date is transmitted in the format DD.MM.YYYY or YYYY-MM-DD.
The name of the form template is given in free form, such as MyForm or Order Form. Field names are also given in free form (preferably meaningful), such as user_name, order_date, or lead_email.
The /get_form POST request sends data like this: f_name1=value1&f_name2=value2
In response, you need to return the name of the form template, if it was found. To find a suitable template, you need to select the one whose fields matched the fields in the sent form. Matching fields are fields that have the same name and value type. There may be more fields in the incoming form than in the template, in which case the template will still be considered appropriate. The most important thing is that all template fields are present in the form. I am using Python Django. my models.py
from django.db import models
class Template(models.Model):
name = models.CharField(max_length=256, verbose_name='Название шаблона')
def __str__(self):
return self.name
class Meta:
verbose_name = 'Шаблон'
verbose_name_plural = 'Шаблоны'
class Field(models.Model):
class Type:
choices = (
('email', "email"),
('date', "date"),
('phone', "phone"),
('text', "text"),
)
template = models.ForeignKey(Template, related_name='sample', verbose_name='Шаблон', on_delete=models.CASCADE)
name_field = models.CharField(max_length=256, verbose_name='Имя поля')
type_field = models.CharField(max_length=5, choices=Type.choices, verbose_name='Тип поля')
def __str__(self):
return self.name_field
class Meta:
verbose_name = 'Поле'
verbose_name_plural = 'Поля'
from .utils import type_form
import json
# Create your views here.
class Templates_views (View):
def post(self, request):
form_fields_types = form_type(request.GET)
form_search_cond = Q() # django.db.models.Q
for k, v in form_fields_types.items():
form_search_cond.add(Q(sample__name_field__contains=k, sample__type_field=v), Q.AND)
template = Template.objects.filter(form_search_cond).annotate(count=Count('id')).order_by('-count')
return JsonResponse(form_fields_types)
def type_v(v):
test = {'email': r'^\[email protected]\w+.\w{2,4}$',
'date': r'^\d\d\.\d\d\.\d{4}$',
'phone': r'^79\s*\d{2}\s*\d{3}\s*\d{2}\s*\d{2}$',
}
for t, r in test.items():
if re.fullmatch(r, v):
return t
return 'text'
def type_form(d):
res = {}
d = d.dict()
for k, v in d.items():
res[k] = type_v(v)
retur
{
"User_name": "text",
"User_email": "email",
"date": "date",
"User_phone": "phone"
}
{"name": "User",
"sample": [
{
"name_field": "User_name",
"type_field": "text"
},
{
"name_field": "User_email",
"type_field": "email"
},
{
"name_field": "User_phone",
"type_field": "phone"
}
]
}
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