简体   繁体   English

Django-从预先创建的列表中选择项目和数量

[英]Django - Select items and quantity from pre-created list

I cannot figure out a suitable solution for what I believe is a beginner problem. 我无法为我认为是初学者的问题找到合适的解决方案。 Any help is highly appreciated. 非常感谢您的帮助。

The website I am building will be used to plan parties. 我正在建立的网站将用于计划聚会。 Two types of people will use it, party organizers and party visitors. 有两种类型的人可以使用它:聚会组织者和聚会访客。 The party organizers are able to create a party and select items to be offered to visitors. 派对组织者可以创建一个派对并选择要提供给访问者的项目。 Items are food and drinks, 1 item = one pizza or one beer. 项目是食物和饮料,一项=一份比萨饼或一份啤酒。

Users will then be able to confirm participation for the party and select which food and drinks they would like to order and how many. 然后,用户将能够确认参加派对的人,并选择他们想要订购的食物和饮料以及数量。 I already created authentication and creation of a party. 我已经创建了身份验证和创建了聚会。

Now, my issues are this: 现在,我的问题是这样的:

  1. How can the organizer create a list of items to be offered to the visitor? 组织者如何创建要提供给访客的物品清单?
  2. How can the visitor select both type of item and quantity? 访问者如何选择这两种类型的项目和数量?

I have tried with the following code (trimmed down): 我已经尝试使用以下代码(已精简):

models.py: models.py:

class ItemType(models.Model):
    """Create items (food and drinks)."""
    name = models.CharField()

class Party(models.Model):
    """Create a party."""
    organizer = models.ForeignKey(User)
    party_name = models.CharField()
    items_to_be_offered = models.ManyToManyField(ItemType)

class PartyRegistration(models.Model):
    """Register for a party / create party registration."""
    party = models.ForeignKey(Party)
    visitor = models.CharField()
    selected_items = models.ManyToManyField()

class ItemSelection(models.Model):
    """Specify which items and number of items the visitor selected."""
    party_registration = models.OneToOneField(PartyRegistration)
    item_type = models.ManyToManyField(ItemType)
    item_quantity = models.PositiveIntegerField()

forms.py forms.py

class VisitorRegisterForm(forms.ModelForm):
    """Form to register a visitor."""

    class Meta:
        model = PartyRegistration
        fields = ['visitor', 'selected_items']

views.py views.py

def VisitorRegister(request, pk):
    """Register Visitor to a specific party."""    
    party = get_object_or_404(Party, pk=pk)    

    if request.method == 'POST':
        form = VisitorRegisterForm(request.POST)
        if form.is_valid():
            name_clean = form.cleaned_data['visitor']
            registration = PartyRegistration(party=party, visitor=name_clean)
            registration.save()
            return HttpResponseRedirect(reverse('index'))
    else:
        form = VisitorRegisterForm()

    context = {'form': form, 'party': party}

    return render(request, 'visitor-register.html', context)

Website is Django 2.0, PostgreSQL. 网站是Django 2.0,PostgreSQL。

You need a ManyToMany relationship with a through model in this case (order matters, or change the order and quote PartyRegistration ): 在这种情况下,您需要一个带有through模型的ManyToMany关系(订单很重要,或更改订单并引用PartyRegistration ):

class PartyRegistration(models.Model):
    """Register for a party / create party registration."""
    party = models.ForeignKey(Party)
    visitor = models.CharField()
    selected_items = models.ManyToManyField(ItemType, through='ItemSelection')

class ItemSelection(models.Model):
    """Specify which items and number of items the visitor selected."""
    party_registration = models.ForeignKey(PartyRegistration)
    item_type = models.ForeignKey(ItemType)
    item_quantity = models.PositiveIntegerField(default=1)

    class Meta:
        unique_together = ('party_registration', 'item_type')

See https://docs.djangoproject.com/en/2.1/topics/db/models/#intermediary-manytomany 参见https://docs.djangoproject.com/zh-CN/2.1/topics/db/models/#intermediary-manytomany

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM