Reputation: 21
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
Reputation: 8837
You can query like this:
single_object=get_object_or_404(Group,name=any_name)
object_id=single_object.pk
Upvotes: 1