kano
kano

Reputation: 21

how to get the id of an entry based on the name in Django?

I have created the following model in Django to store my groups in a database:

class Group(models.Model):
    name = models.CharField(max_length=1000)

Now I would like to get the id of a group by its name in my views.py. How do I do that? with the Group.objects.get() method I can filter by name, but there I get the whole row without the id.

Upvotes: 0

Views: 822

Answers (1)

Sunderam Dubey
Sunderam Dubey

Reputation: 8837

You can query like this:

single_object=get_object_or_404(Group,name=any_name)
object_id=single_object.pk

Upvotes: 1

Related Questions