user,package,travellers
フィールド(many2manyfield)CustomizeRequest
を除いて、フィールドはモデル用に保存されています。destinations
どんな助けでも大歓迎です!!!
models.py
class CustomizeRequest(models.Model):
user = models.ForeignKey(User)
package = models.CharField(max_length =20,null=True,blank=True)
travellers = models.CharField(max_length=2)
destinations = models.ManyToManyField(DestinationChoices)
def __str__(self):
return self.travellers
ビュー.py
def packageDetails(request,slug):
package = Package.objects.get(slug=slug)
if request.method=="POST":
form = CustomizeRequestForm(request.POST)
if form.is_valid():
customize_request = form.save(commit=False)
customize_request.user = request.user
customize_request.package = package
customize_request.save()
form.save_m2m()
else:
print("errorerrorerrorerrorerror")
else:
print("notPostData")
crform = CustomizeRequestForm()
elements = CustomizeRequest.objects.filter(user=request.user,package=package)
destinations = DestinationChoices.objects.all()
context = {'package': package,'crform':crform,'elements':elements,'destinations':destinations }
return render(request,'tour/packageDetails.html',context)
フォーム.py
nTravellerCHOICES = (('1', '1',), ('2', '2',), ('3', '3',), ('4', '4',), ('5', '5',), ('6', '6',), ('7', '>6',))
class CustomizeRequestForm(forms.ModelForm):
travellers = forms.ChoiceField(widget = forms.RadioSelect,choices = nTravellerCHOICES)
destinations = forms.ModelMultipleChoiceField(required=False,queryset=DestinationChoices.objects.all())
class Meta:
model = CustomizeRequest
fields = ("travellers","destinations",)
保存の問題であるため、問題はモデルまたはビューコードにあると確信しているため、テンプレートコードを貼り付けません