Модель
from django.db import models
from django.contrib.auth.models import User
class Atc(models.Model):
id = models.AutoField(primary_key=True)
atc_name = models.CharField(unique=True,max_length=100)
email = models.EmailField(null=True,max_length=50)
phone = models.CharField(null=True,max_length=50)
class Meta:
db_table = u'atc'
permissions = (("read_atc","право на чтение"),("add_atc","право на добавление"),("change_atc","право на изменение"),("delete_atc","право на удаление"),)
class Atc2Users(models.Model):
id = models.AutoField(primary_key=True)
atc = models.ForeignKey(Atc)
user = models.ForeignKey(User,unique=True)
class Meta:
db_table = u'atc2users'
permissions = (("read_atc2users","право на чтение"),("add_atc2users","право на добавление"),("change_atc2users","право на изменение"),("delete_atc2users","право на удаление"),)
#coding:utf-8
from django import forms
from django.forms import ModelForm
from tss.journals.models import Atc
from tss.journals.models import Atc2Users
class A2U(ModelForm):
atc = forms.ChoiceField(label='ЭТЦ:')
user = forms.ChoiceField(label='Пользователь:')
class Meta:
model = Atc2Users
fields = ('atc','user')
#coding:utf-8
from django.shortcuts import render_to_response
from tss.forms.spr import A2U
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required
from tss.journals.models import Atc
from tss.choices import USER_LIST
from tss.choices import ATC_LIST
from tss.journals.models import Atc2Users
### --- Добавление записи ---
def add(request):
action = 'new'
error = ''
## --- Для заполнения ----
try:
spr = A2U(request.POST)
user_kod = request.POST['user']
atc_kod = request.POST['atc']
u = User.objects.get(pk=user_kod)
a = Atc.objects.get(pk=atc_kod)
if spr.is_valid():
a2u = Atc2Users()
a2u.atc = a
a2u.user = u
a2u.save()
return render_to_response('atc2users.html',{'a2u1':get_list()[0:39],'a2u2':get_list()[40:89],'error':error})
else:
error = 'Выбирете значение!'
except:
spr = A2U()
spr.fields['user'].choices = USER_LIST(first=1)
spr.fields['atc'].choices = ATC_LIST(first=1)
return render_to_response('atc2useradd.html',{'spr':spr,'error':error,'action':action})
Выберите корректный вариант. 2 нет среди допустимых значений.