django ModelChoiceField设置来自views.py的查询集

时间:2017-07-04 19:16:57

标签: python django django-forms django-views

我是django的新手,需要一些帮助。

forms.py

import floppyforms as ff
from django import forms
from django.contrib.auth.models import User

from .models import Prodavnica, Racun, Transakcija
...


class IzdatakForm(forms.ModelForm):
    izdatak = forms.DecimalField(max_digits=20,
                              decimal_places=2,
                              widget=forms.NumberInput(attrs={'min': '0', 'step': '0.01'}))
    datum = ff.DateField(widget=ff.DateInput(format='yyyy-mm-dd'))
    vreme = ff.TimeField(widget=ff.TimeInput(attrs={'step': '60'}, format='HH-MM'))
    prodavnica = forms.ModelChoiceField(required=True,queryset=Prodavnica.objects.all())

    class Meta:
        model = Transakcija
        fields = ['izdatak', 'datum', 'vreme', 'racun', 'prodavnica']

models.py

from django.contrib.auth.models import User
from django.db import models

from django.urls import reverse


# Create your models here.


class Prodavnica(models.Model):
    author = models.ForeignKey(User, on_delete=models.PROTECT)
    naziv = models.CharField(max_length=50, unique=True, blank=False)
    adresa = models.CharField(max_length=200, blank=True)
    telefon = models.CharField(max_length=50, blank=True)
    foursquare = models.URLField(blank=True)

    def get_absolute_url(self):
        return reverse(viewname='finance_detail_shop', kwargs={'pk': self.pk})

    def __str__(self):
        return self.naziv


class Transakcija(models.Model):
    korisnik = models.ForeignKey(User, on_delete=models.PROTECT)
    suma = models.DecimalField(max_digits=20, decimal_places=2, default='0.0')
    izdatak = models.DecimalField(max_digits=20, decimal_places=2, default='0.0', null=True)
    dodatak = models.DecimalField(max_digits=20, decimal_places=2, default='0.0', null=True)
    prodavnica = models.ForeignKey('Prodavnica', related_name='prodavnica', blank=True, null=True)
    racun = models.ForeignKey('Racun', related_name='Racun')
    datum = models.DateField()
    vreme = models.TimeField()

    def get_absolute_url(self):
        return reverse(viewname='finance_detail_transaction', kwargs={'pk': self.pk})



    def __str__(self):
        return '%s %s (%s %s)' % (self.pk, self.suma, self.datum,self.vreme)



class Racun(models.Model):
    vlasnik = models.ForeignKey(User, on_delete=models.PROTECT, related_name='vlasnik')
    ovlacena_lica = models.ManyToManyField(User, related_name='ovlaceno_lice')
    naziv = models.CharField(max_length=50, unique=True)
    broj = models.IntegerField(unique=True)

    def get_absolute_url(self):
        return reverse(viewname='finance_detail_account', kwargs={'pk': self.pk})

    def __str__(self):
        return self.naziv

views.py

class TransakcijaDodatakCreate(CreateView):
    model = Transakcija
    form_class = DodatakForm


    def get_form(self, form_class=None):
        user = self.request.user
        racun = Racun.objects.filter(Q(vlasnik=user) | Q(ovlacena_lica=user))
        self.fields['racun'].queryset = racun
        return super().get_form(form_class)

    def form_valid(self, form):
        user = self.request.user
        form.instance.korisnik = user
        form.instance.izdatak = None
        form.instance.prodavnica = None
        return super(TransakcijaDodatakCreate, self).form_valid(form)

    template_name = 'finance/transakcija/dodaci/form.html'

返回错误'NoneType'对象不可订阅。

我试图只将ModelChoiceField racun的查询集设置为只有用户拥有的那些racuns。

如果可以从CreateView设置ModelChoiceField的查询集,那我就会徘徊?

1 个答案:

答案 0 :(得分:2)

该代码属于表单上的__init__方法,而不属于视图。

class DodatakForm(forms.ModelForm):
    ...

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(DodatakForm, self).__init__(*args, **kwargs)
        if user:
            racun = Racun.objects.filter(Q(vlasnik=user) | Q(ovlacena_lica=user))
            self.fields['racun'].queryset = racun

并且在视图中,不要覆盖get_form,而是定义get_form_kwargs

def get_form_kwargs(self, *args, **kwargs):
    kwargs = super(TransakcijaDodatakCreate, self).get_form_kwargs(*args, **kwargs)
    kwargs['user'] = self.request.user
    return kwargs