P
P
paevlk20072014-09-20 16:57:12
Django
paevlk2007, 2014-09-20 16:57:12

How to get different initial models in forms if virew is one, form is one, different urls?

Hey!
models.py

class Models_view(models.Model):
    view_name = models.CharField(max_length=100)

view_name = View1
view_name = View2
class Models_view(models.Model):
    name = models.CharField(max_length=100)
    models_views =  models.ForeignKey(Models_view)

forms.py
class ModelForm(forms.ModelForm):

    class Meta:
        model = Model

    def __init__(self, *args, **kwargs):
        super(ModelForm, self).__init__(*args, **kwargs)
        self.fields["models_views"].initial = Models_view.objects.get(id=1)

views.py
def add(request, template_name="lbforum/form_add.html" ):
    if request.method == 'POST':
        f = ModelForm(request.POST)
        if f.is_valid():
            f.save()
            return HttpResponseRedirect(reverse("models"))
    else:
        f = ModelForm()
    ctx = {'form': form}
    return render(request, template_name, ctx)

urls.py
url(r'^add1/$', views.accessories_add, name='accessories_add'),
url(r'^add2/$', views.accessories_add, name='accessories_add1'),

When entering the link url(r'^add1/$', views.add, name='add1'), is required in when opening a new form forms
self.fields["models_views"].initial = Models_view.objects.get(id=1),
and aturl(r'^add2/$', views.add, name='add2')
self.fields["models_views"].initial = Models_view.objects.get(id=2)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
yttrium, 2014-09-20
@yttrium

Learn python. How classes work. Learn databases (nomal forms). Learn the basics of djanga
Add and pass an argument to the form's initialization function. But your models are somehow lame.
and as the previous speaker noted, the designation of variables and designations of classes in python are regulated.

A
Alexey Bukin, 2014-09-21
@abukin

In views.py, a good need is:

from django.shortcuts import get_object_or_404
...
url_name = request.resolver_match.url_name
if url_name == 'add1':
    pk = 1
elif url_name == 'add2':
    pk = 2

obj = get_object_or_404(PleaseModelNormalName, pk=pk)
f = ModelForm(request.POST, initial={'models_views': obj})

P/S Python has many options not to repeat itself. And yes, read the jangga and python conventions. PEP8 is here to help.

P
paevlk2007, 2014-09-20
@paevlk2007

Problem solved:
models.py

class Models_view(models.Model):
    view_name = models.CharField(max_length=100)

class Model(models.Model):
    name = models.CharField(max_length=100)
    models_views =  models.ForeignKey(Models_view)

forms.py
class ModelForm(forms.ModelForm):
    class Meta:
        model = Model

views.py
def add(request, view=None, template_name="lbforum/form_add.html" ):
    if request.method == 'POST':
        f = ModelForm(request.POST)
        if f.is_valid():
            f.save()
            return HttpResponseRedirect(reverse("models"))
    else:
        if view == 1:
            f = ModelForm(initial={'models_views': Models_view.objects.get(id=1)}) # в этом вся фишка
        else:
            f = ModelForm(initial={'models_views': Models_view.objects.get(id=2)}) # в этом вся фишка
    ctx = {'form': f}
    return render(request, template_name, ctx)

urls.py
url(r'^add1/$', views.add, {'view': 1}, name='add1'), 
url(r'^add2/$', views.add, name='add2'),

Now there are two links to one views, the first form is created and filled with (initial) field models_views with id=1, the second form with models_views id=2.
Maybe there is a better solution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question