根据先前的字段选择过滤表单中的选择-Django

时间:2019-04-08 11:23:07

标签: django python-3.x

快速总结一下,我有一个应用程序,可让俱乐部注册并执行不同的任务。功能之一是调度/名册。目前,我有一个表格可以增加时间。俱乐部页面根据最初选择的俱乐部创建会话密钥。

我的表单包括: club_id-我已根据登录用户进行隐藏和初始化。 pitch_id-当前正在显示与所有俱乐部相关的所有球场,但是我需要它仅根据club_id的外键显示球场

不胜感激。

form.py

from django import forms
from clubkit.roster.models import RosterId
import datetime
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _


class RosterForm(forms.ModelForm):

class Meta():
    model = RosterId
    fields = ('club_id', 'pitch_id', 'team_id', 'date',
              'start_time', 'finish_time', 'reoccuring_event',)
    widgets = {
        'date': forms.DateInput(attrs={'id': 'datepicker'})
    }

    def clean_date(self):
        date = self.clean_date['date']
        if date < datetime.date.today():
            raise ValidationError(_('Date cannot be in the past.'))
        return date

def __init__(self, *args, **kwargs):
    super(RosterForm, self).__init__(*args, **kwargs)
    self.fields['club_id'].widget = forms.HiddenInput()

models.py为音高

class Pitch(models.Model):
club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE, related_name="pitches")
pitch_name = models.CharField(max_length=30)
PITCH_SIZES = (
    ('S', 'Small'),
    ('M', 'Medium'),
    ('L', 'Large'),
)
PITCH_TYPE = (
    ('1', 'Outdoor'),
    ('2', 'Indoor'),
)
pitch_size = models.CharField(max_length=1, choices=PITCH_SIZES)
pitch_type = models.CharField(max_length=1, choices=PITCH_TYPE)
open_time = models.TimeField(default='09:00')
close_time = models.TimeField(default='22:00')
RENT_TYPE = (
    ('0', 'Not Available To Rent'),
    ('1', 'Available To Rent'),
)
rental = models.CharField(max_length=1, choices=RENT_TYPE)
rental_price = models.DecimalField(default=0.00, max_digits=6, decimal_places=2)
max_people = models.IntegerField(null=True)

def __str__(self):
    return self.pitch_name

models.py(用于名册)

from django.db import models
from clubkit.clubs.models import ClubInfo, Pitch, Team


# Model to store roster information
class RosterId(models.Model):
club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
pitch_id = models.ForeignKey(Pitch, on_delete=models.CASCADE)
team_id = models.ForeignKey(Team, on_delete=models.CASCADE)
date = models.DateField(max_length=8)
start_time = models.TimeField(default='')
finish_time = models.TimeField(default='')
reoccuring_event = models.BooleanField(default=False)

views.py

class ClubRoster(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'roster.html'

# Get method to retrieve current roster information and form
def get(self, request):
    if request.user.is_authenticated:
        club_pk = request.session.get('pk')
        club_info = ClubInfo.objects.filter(user=request.user).first()
        reoccuring_event = RosterId.objects.filter(reoccuring_event=True, club_id=club_pk)
        inital_data = {
            'club_id': club_info,
        }
        form = RosterForm(initial=inital_data)
        roster = RosterId.objects.filter(club_id=club_pk)
        return Response({'form': form,
                         'roster': roster,
                         'club_pk': club_pk,
                         'reoccuring_event': reoccuring_event
                         })

1 个答案:

答案 0 :(得分:0)

听起来像您在寻找对表单字段查询的某种操作。也许this answer将提供进一步的帮助。

结果可能看起来像这样。


from django import forms
from clubkit.roster.models import RosterId, Pitch
import datetime
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _


class RosterForm(forms.ModelForm):

    class Meta():
        model = RosterId
        fields = ('club_id', 'pitch_id', 'team_id', 'date',
                  'start_time', 'finish_time', 'reoccuring_event',)
        widgets = {
            'date': forms.DateInput(attrs={'id': 'datepicker'})
        }

    def clean_date(self):
        date = self.clean_date['date']
        if date < datetime.date.today():
            raise ValidationError(_('Date cannot be in the past.'))
        return date

    def __init__(self, *args, **kwargs):
        super(RosterForm, self).__init__(*args, **kwargs)
        self.fields['club_id'].widget = forms.HiddenInput()

        instance = kwargs.get('instance', None)
        if instance:
            self.fields['pitch_id'].queryset = Roster.objects.filter(club_id=instance.club_id)

好吧,或者将instance调整为您的“初始”。