Discussion:
How to autosave Foreignkey value inside my second Model with the id of my first model object.??
Anoop Sharma
2018-11-25 11:31:49 UTC
Permalink
I have two models . Appname and Adspace ,I have a Foreignkey object
in second model which connects to my first model. Here is the Code


models.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=Adspace



Now 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+***@googlegroups.com.
To post to this group, send email to django-***@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.
Yavin Aalto Arba
2018-11-25 15:39:03 UTC
Permalink
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.
https://stackoverflow.com/questions/569468/django-multiple-models-in-one-template-using-forms/575133#575133
Post by Anoop Sharma
I have two models . Appname and Adspace ,I have a Foreignkey object
in second model which connects to my first model. Here is the Code
models.py
name=models.CharField(max_length=150,blank=False,null=False,help_text='Add your new App')
return self.name
return reverse("dashapp:space",kwargs={'pk':self.pk})
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)
return self.ad_space
return reverse("dashapp:view")
modelForm
model=Adspace
fields=('ad_space',)
Views.py
form_class=forms.AdspaceForm
model=Adspace
Now 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
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
<https://groups.google.com/d/msgid/django-users/4a23635c-1d6c-4dba-b011-778c1548f938%40googlegroups.com?utm_medium=email&utm_source=footer>
.
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+***@googlegroups.com.
To post to this group, send email to django-***@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.
Continue reading on narkive:
Loading...