Skip to content Skip to sidebar Skip to footer

How Do I Have Two Fields For A Choice?

I'm writing a website in Django, and I want to have two blogs. For each blog I need three variables: a name (that I choose from when I write my post in the admin part - do I unders

Solution 1:

You should use a foreignkey relationship to new model called for example Blog.

Example:

classBlog(models.Model):
    title = models.CharField(max_length=20)
    url = models.URLField()

classPost(models.Model):    

    blog = models.ForeignKey(Blog)

To access the data:

post = Post.objects.get(id=1) 
post.blog.title

You should access the data in your template.

View Example:

defpostview(request):
    return render('template_xyz.html', {'object': Post.objects.get(id=1) }

Template Example:

<h1>{{ object.blog.title }} </h1>

Post a Comment for "How Do I Have Two Fields For A Choice?"