from itertools import product
d = {
'param_abdomen': ['thin', 'normal', 'large'],
'param_chest': ['strong', 'normal', 'corpulent'],
'param_shoulders': ['straight', 'normal', 'fallen'],
'param_stance': ['straight', 'normal', 'curved']
}
keys = sorted(d)
variants = [dict(zip(keys, p)) for p in product(*(d[k] for k in keys))]
@require_POST
@login_required
@transaction.atomic()
@reversion.create_revision()
def add_something(request):
form = SomeModelForm(request.POST)
if form.is_valid():
instance = form.save()
reversion.set_user(request.user)
reversion.set_comment(u'Что-то добавлено')
return redirect(instance)
else:
return render(request, 'form.html', {'form': form})
versions = Version.objects.get_for_object(instance)
for n, version in enumerate(versions, start=1):
print(u'В версии №{} от {}'.format(n, version.revision.date_created))
for field_name, field_value in version.field_dict.items():
print(u'поле "{}" имело значение "{}"'.format(field_name, field_value))
print()
versions = Version.objects.get_for_object(instance)
versions[0].revision.revert()
class MessageView(CreateView):
model = Message
form_class = MessageForm
template_name = 'mssgs/message_form.html'
success_url = 'messages'
def get_initial(self):
initial = super(MessageView, self).get_initial()
group_id = self.kwargs['group']
initial['group'] = Group.objects.get(pk=group_id)
return initial
python -m pip install -U pip
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('mainwindow.ui', self)
import json
from django import forms
class SomeForm(forms.Form):
name = forms.CharField(label=u'Name', max_length=25)
def some_view(request):
json_string = request.POST.get('json_data')
form_data = json.loads(json_string)[0]
f = SomeForm(form_data)
if f.is_valid():
name = f.cleaned_data['name']
# ... Работа с данными ...
else:
# ... Обработка ошибки ...
from django.forms.models import model_to_dict
model = Chat2.objects.get(id=20)
field_values = model_to_dict(model)
field_values = Chat2.objects.get(id=20).values()
UPDATE pg_database SET datallowconn = 'false' WHERE datname = 'database_name';
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'database_name' AND pid <> pg_backend_pid();
DROP DATABASE database_name;
def form_invalid(self, form):
form.add_error(None, u'Вы кто такие? Я вас не звал!');
return super(LoginFormView, self).form_invalid(form)
select
t1.name, array_agg(t2.skills)
from table1 as t1
inner join table2 as t2
using (id)
group by t1.name;