Python 使用postgres arrayfield中的值在django视图中呈现下拉列表

Python 使用postgres arrayfield中的值在django视图中呈现下拉列表,python,django,postgresql,django-forms,django-postgresql,Python,Django,Postgresql,Django Forms,Django Postgresql,以下是模型中的一个示例: class Shipment(models.Model): shipment_id = models.BigAutoField(null=False, primary_key=True) potential_shipping_dates = ArrayField(models.DateField(), verbose_name='Ship Dates', null=True) 以下是我在形式上的尝试: class ShippingForm(forms.

以下是模型中的一个示例:

class Shipment(models.Model):
    shipment_id = models.BigAutoField(null=False, primary_key=True)
    potential_shipping_dates = ArrayField(models.DateField(), verbose_name='Ship Dates', null=True)
以下是我在形式上的尝试:

class ShippingForm(forms.Form):
    potential_shipping_dates = forms.ModelChoiceField(queryset=Shipment.objects.all())
    def __init__(self, *args, **kwargs):
        super(ShippingForm, self).__init__(*args, **kwargs)
下面是我的表单添加到上下文中的位置:

context['shippingForm'] = ShippingForm(initial=??what_goes_here_maybe??)

我的表单呈现得很好,但我想显示一个下拉列表,其中包含每个选项的日期。

好的,这有点复杂,但我想我理解您试图做什么,以及哪里出了问题

因此,您有一个装运模型,每个装运实例都有一个字段,其中包含一些不同的
潜在装运日期

假设您有2批货:

IN : ship1 = Shipment.objects.first()
OUT:

IN : ship1.potential_shipping_dates
OUT: ['01/01/2021', '02/02/2021']

IN : ship2 = Shipment.objects.last()
OUT:

IN : ship2.potential_shipping_dates
OUT: ['03/03/2021', '04/04/2021']
现在,您是否希望下拉列表将所有4个日期作为可能的日期,并选择
装运

还是在表单中选择装运后选择日期

^^在评论中回答

好的,您需要将实例传递到表单:

视图.py

# Inherit from Django's UpdateView to have `instance` passed through to the form
class ShippingFormView(UpdateView):
    model = Shipment
    form_class = ShippingForm


# Or if you don't want to inherit from inherit from UpdateView
class ShippingFormView(Blah):
    model = Shipment
    form_class = ShippingForm

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['instance'] = self.get_object()
        return kwargs


# Or if you're using function based views
def shipping_form_view(request, pk):
    shipment = get_object_or_404(Shipment, pk=pk)
    form = ShippingForm(request, instance=shipment)
    ...

forms.py

# Inherit from Django's UpdateView to have `instance` passed through to the form
class ShippingFormView(UpdateView):
    model = Shipment
    form_class = ShippingForm


# Or if you don't want to inherit from inherit from UpdateView
class ShippingFormView(Blah):
    model = Shipment
    form_class = ShippingForm

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['instance'] = self.get_object()
        return kwargs


# Or if you're using function based views
def shipping_form_view(request, pk):
    shipment = get_object_or_404(Shipment, pk=pk)
    form = ShippingForm(request, instance=shipment)
    ...

class ShippingForm(forms.Form):
潜在装运日期=forms.ChoiceField(选项=[])
定义初始化(self,*args,instance,**kwargs):
超级(发货表单,自我)。\uuuuu初始值(*args,**kwargs)
self.fields['potential\u shipping\u dates'].choices=((dt,dt)用于实例中的dt.potential\u shipping\u dates)

modelcoocefields
用于选择
对象,而不是其中的属性。

因此,我希望从指定的装运实例中选择一个日期…就像我有一个装运,我希望能够从表单中更改装运日期一样,使用在发货实例实例化时添加的多个不同日期Kay答案在上面好的,这看起来很好,是的,我如何成功地将实例传递到表单?非常感谢你@一般情况下,如果表单有效
表单。已清理的数据['potential\u shipping\u dates']
将为您提供所选日期。您将在视图中完成此日期的剩余操作。好了,开始@generalbar