question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access Django Model primary key

See original GitHub issue

Hi,

I would like to do something like this:

allIngredients {
  edges {
    node {
      id
      pk
      name
    }
  }
}

where the result would be something like this:

{
  "id": "Q29tcGFueU5vZGU6MQ==",
  "pk": 42,
  "name": "carrots"
}

Is this possible? How could it be achieved?

Perhaps relatedly, how could I expose model properties?

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bcbcommented, Nov 5, 2016

Can I just clarify - so id retrieves the primary key, unless the Node interface is used in which case it’s the relay node id?

1reaction
syrusakbarycommented, Nov 3, 2016

The id retrieves automatically the Primary Key in the Django Model instance by default, as @bcb pointed.

You can expose model properties by creating a field for the property. For example, if your Model looks something like:

# Here the Django Model
class MyModel(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    @property
    def full_name(self):
        return "{} {}".format(self.first_name, self.last_name)

# Here the Graphene Type
class MyModelType(DjangoObjectType):
    class Meta:
        # This will automatically convert the first_name and last_name fields to GraphQL fields
        model = MyModel

     full_name = graphene.String()
     # If we don't specify the resolver, it will try to resolve using the instance.full_name
Read more comments on GitHub >

github_iconTop Results From Across the Web

Models | Django documentation
Models ¶. A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the...
Read more >
Get name of primary field of Django model
In Django, every model has a pseudo attribute pk , that points to the field that is declared as primary key. class TestModel(models....
Read more >
primary_key - Django Built-in Field Validation
Built-in Field Validations in Django models are the default validations that come predefined to all Django fields. Every field comes in with ...
Read more >
notes/models.md at master · pshrmn/notes
By default, Django adds an id field to each model, which is used as the primary key for that model. You can create...
Read more >
Model instance reference — Django 4.1.4 documentation
Regardless of whether you define a primary key field yourself, or let Django supply one for you, each model will have a property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found