python - getting the id from a foreignkey relationship django -


i want id or pk of foreignkey relationship post_comment i've tried many different ways catch , not have result, please guys give me hand in situation

in views.py

class createcomment(view):     form_class = commentform     template_name = "createcomment.html"      def get(self, request):         form = self.form_class(none)         return render(request, self.template_name, {'form':form})      def post(self, request):         obj = self.form_class(none)         obj.title_comment = self.request.post['title_comment']         obj.body_comment = self.request.post['body_comment']         obj.post_comment = self.pk         obj.save() 

in models.py

class comment(models.model):     user_comment = models.foreignkey("auth.user")     title_comment = models.charfield(max_length=50)     body_comment = models.textfield()     timestamp_comment = models.datetimefield(auto_now=true)     post_comment = models.foreignkey("post", null=true)     status_comment = models.booleanfield(default=true)      def __unicode__(self):         return unicode(self.title_comment)      def __str__(self):         return self.title_comment 

you can pass primary key in url, , use in class 1 way.

kwargs.get(pk name)

you change post to:

def post(self, request, *args, **kwargs) 

you can't assign obj.post_comment = kwargs.get(pk) have object.

post.objects.get(pk = pk)

you might want consider renaming fieldname_comment fieldname models fields. seems bit redundant have _comment on every single field in comment model.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo