The whole point of a foreign key is that it's external\independent the related model - and you are going against that by giving the user the ability to enter it willy-nilly in one form. If you want to have a 2 key combo as primary key you can use unique_together without the second model as a FK (https://docs.djangoproject.com/en/2.1/ref/models/options/#unique-together)
If you really insist on this design...You could make two forms (one for the appname and one for the other model without the appname) and set the saving of the second form with a save(commit=False). then, set the FK according to the first saved form input. It's pretty ugly imho but it can be done, cf.
On Sun, 25 Nov 2018 at 15:49, Anoop Sharma <anoopsharma527@gmail.com> wrote:
--I have two models . Appname and Adspace ,I have a Foreignkey object
in second model which connects to my first model. Here is the Codemodels.py
class Appname(models.Model):
name=models.CharField(max_length=150,blank=False,null=False,help_text='Add your new App')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("dashapp:space",kwargs={'pk':self.pk})
class Adspace(models.Model):
ad_space=models.CharField(max_length=150,blank=False,null=False)
app=models.ForeignKey('Appname', related_name='appnames',default=None, on_delete=models.CASCADE)
PID_TYPE = (
('FN','FORMAT_NATIVE'),
('FNB','FORMAT_NATIVE_BANNER'),
('FI','FORMAT_INTERSTITIAL'),
('FB','FORMAT_BANNER'),
('FMR','FORMAT_MEDIUM,RECT'),
('FRV','FORMAT_REWARDED_VIDEO'),
)
format_type=models.CharField(max_length=3,choices=PID_TYPE,default='FN',blank=False, null=False)
def __str__(self):
return self.ad_space
def get_absolute_url(self):
return reverse("dashapp:view")modelForm
class AdspaceForm(forms.ModelForm):
class Meta:
model=Adspace
fields=('ad_space',)Views.py
class space(LoginRequiredMixin,CreateView):
form_class=forms.AdspaceForm
model=AdspaceNow I.m unable to fill my form as it says it app_id cannot be null
and i'm allowing user to input it. I want that the app which got clicked
has its ID in the url (As I'm passing it through there).
What should i do that it gets autosave with the same id number by itself
that is being passed in the url.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4a23635c-1d6c-4dba-b011-778c1548f938%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B%2Be-ZU%3DkLUBGPrZQQ9hoXDemM1RJtB8aorj%2B_GhXhDNwEiaLw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment