Вам не нужно иметь доступ к нему в контроллере, чтобы использовать его в представлении. Это своего рода точка фильтров. Если вы действительно нуждались в нем в контроллере, вы можете запросить его самостоятельно, попросив ввести filterTempFilter
. Или вы можете ввести провайдера $ filter и запросить у него свой фильтр.
Вероятно, необходимо удалить поле ManyToMany из модели Client или иначе тщательно исключить его из формы. К сожалению, виджет по умолчанию для поля ManyToMany не может заполнить Модель ClientGroupe правильно (даже если недостающее поле, dt, было установлено на autonow=True). Это - что-то, что необходимо будет или вспыхнуть в другую форму или обработать в представлении.
При сохранении формы Вы сохраняете Объект клиента. Теперь, если Вы хотите присвоить клиент группе, необходимо сделать это:
clientgroupe = ClientGroupe.objects.create(client=client_instance, groupe=groupe_instance, dt=datetime.datetime.now())
, где client_instance и groupe_instance Ваш клиент и groupe objets.
If you use the save method right now, Django will try to save using the manager (which Django doesn't allow). Unfortunately, the behavior you want is a little bit trickier than what ModelForm
does by default. What you need to do is create a formset.
First of all, you will need to change the options of your ClientForm
so that it doesn't display the groupes
attribute.
class ClientForm(ModelForm):
class Meta:
model = Client
exclude = ('groupes',)
Next, you must change the view to display the formset:
from django.forms.models import inlineformset_factory
def modifier(request, id):
client = Client.objects.get(id=id)
form = ClientForm(instance = client)
# Create the formset class
GroupeFormset = inlineformset_factory(Client, Groupe)
# Create the formset
formset = GroupeFormset(instance = client)
dict = {
"form": form
, "formset" : formset
, "instance" : client
}
if request.method == "POST":
form = ClientForm(request.POST, instance = client)
formset = GroupeFormset(request.POST, instance = client)
if form.is_valid() and formset.is_valid():
client_mod = form.save()
formset.save()
id = client_mod.id
return HttpResponseRedirect(
"/client/%(id)s/?err=success" % {"id" : id}
)
else:
return HttpResponseRedirect(
"/client/%(id)s/?err=warning" % {"id" : id}
)
return render_to_response(
"client/modifier.html"
, dict
, context_instance=RequestContext(request)
)
And obviously, you must also tweak your template to render the formset.
If you need any other advice on formsets, see these articles: