Hi, Can you be more specific about what you want to achieve, you want to save form with foreign key value ? Are you taking input for foreign key id or something ?
Cheers On Thursday, 12 March 2020 03:24:07 UTC+5:30, abderrahim ariche wrote: > > > > hello i guys i want save form with foreignkey thee value what i want on > this foreign key the id of the product page the form has filled and this > task need to be hidden like the value of of the form is page id > > like this flow: > > the product_id= 'value of current page id' > > and i think i need to use __init__ on form.py as filter and i not > understand what this really work i try but no result thanks for attention I > appreciate your answers > > thank you > > > other field = no problem on this work great > form = Modelform(request.POST) > > Modelform.Product.filter(id=p_id) > if form.is_valid(): > post = form.save(commit=False) > > #p_id =Product.objects.filter( id=p_id) > #Modelform(initial={'p_id':p_id}) > post.save() > form = Modelform() > > the models: > from django.db import models > class Product(models.Model): > title = models.CharField(max_length=50 , default='product title') > price = models.CharField(max_length=10 , default= int('0')) > delPrice = models.CharField(max_length=10, default=int('0')) > img1 = models.ImageField(upload_to='product_gallry',default='img') > img2 = models.ImageField(upload_to='product_gallry',default='img') > img3 = models.ImageField(upload_to='product_gallry',default='img') > description = models.TextField(max_length=5000 , default='description') > Video_Link = models.TextField(max_length=5000 , default='Video_Link') > def __unicode__(self): > return u'%d' % self.id > class Form (models.Model): > Product = models.ForeignKey(Product,null=True, > blank=True,on_delete=models.SET_NULL) > name = models.CharField(max_length= 100 , default=None) > number = models.CharField(max_length=13,default=None) > adresse = models.CharField(max_length=500,default=None) > city = models.CharField(max_length=20, default=None) > > > > > the views: > > from .form import Modelform > from .models import Product, Form > from django.shortcuts import render > from django.shortcuts import get_object_or_404 > from django.utils import timezone > def p_page(request,p_id): obj = Product.objects.all() > template = "index.html" > product = get_object_or_404(obj,id=p_id) > if request.method =='POST': > form = Modelform(request.POST) > Modelform.Product.filter(id=p_id) > if form.is_valid(): > post = form.save(commit=False) > #p_id =Product.objects.filter( id=p_id) > #Modelform(initial={'p_id':p_id}) > post.save() > form = Modelform() > context = { 'form': form, 'product': product, } > return render(request,template, context,) > the form.py: > > from django import forms > from . import models > from .models import Product > class Modelform(forms.ModelForm): > Product = forms.ModelChoiceField(queryset=Product.objects.all(), ) > name = forms.CharField(max_length=100, > widget=forms.TextInput( attrs={ 'style': ' height: 20%; > display: inline-block; width: 80%; text-align: center; font-size: 30px; > margin: 7px 2.5%; float: right; background: white; color: #c50d2c;', > 'placeholder': 'Write your name here' } ) ) > number = forms.CharField(max_length=13, > widget=forms.TextInput( attrs={ 'style': ' height: 20%; > display: inline-block; width: 80%; text-align: center; font-size: 30px; > margin: 7px 2.5%; float: right; background: white; color: #c50d2c;', > 'placeholder': 'Write your name here' } ) ) > adresse = forms.CharField(max_length=500, > widget=forms.TextInput( attrs={ 'style': ' height: 20%; > display: inline-block; width: 80%; text-align: center; font-size: 30px; > margin: 7px 2.5%; float: right; background: white; color: #c50d2c;', > 'placeholder': 'Write your name here' } ) ) > city = forms.CharField(max_length=20, widget=forms.TextInput( attrs={ > 'style': ' height: 20%; display: inline-block; width: 80%; text-align: > center; font-size: 30px; margin: 7px 2.5%; float: right; background: white; > color: #c50d2c;', 'placeholder': 'Write your name here' } ) ) > class Meta: > model = models.Form > fields = ['name','number','adresse','city','Product'] > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/539cd2ad-d647-4891-a1ad-f0280c543de5%40googlegroups.com.