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.

Import name instead of ID in a foreign key field

See original GitHub issue

Hi!

I have this model:

models.py

`class User(models.Model):
    user_name = models.CharField(max_length=50, blank=False, verbose_name='Nome')
    user_login = models.CharField(max_length=50, blank=True, verbose_name='Login de Rede')
    user_position = models.ForeignKey('UserPositionDatabase', on_delete=models.PROTECT, max_length=50, blank=True, verbose_name='Posição', default="") 
    user_departament = models.ForeignKey('Departament', on_delete=models.PROTECT, null=False, blank=True, verbose_name='Departamento')
    user_manager = models.CharField(max_length=20, verbose_name='Gestor Imediato', default="")
    user_locality = models.ForeignKey('Locality', on_delete=models.PROTECT, blank=True, null=False, verbose_name='Localidade')`

when I export the department which is a foreign key, the result is the id instead of the departament name. How do I change?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
samatharkarcommented, Jun 19, 2021

When I tried this,

It said django.core.exceptions.FieldDoesNotExist: Vendor.added_by_user.name: User has no field named 'name'

My code is :

added_by_user = models.ForeignKey(User, on_delete=models.PROTECT, related_name= "vendors", verbose_name="Added By User") @deki90to any idea?

1reaction
matthewhegartycommented, Aug 14, 2020

That is almost correct. It must be fields not field. And don’t include the ... bit (replace that with any other field names that you want to export).

If you are a beginner (and you don’t already do this) then may I suggest:

  • read the documentation carefully
  • use an IDE like PyCharm and use the debugger.

This will save you many hours in trying to fix issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get foreignkey field name instead of id in django rest ...
I am getting api like this.. Here i am getting id instead of company_name. [ { "id": 1, "name": ...
Read more >
How to get foreignkey field name instead of id in django rest ...
Django : How to get foreignkey field name instead of id in django rest framework [ Beautify Your Computer ...
Read more >
Change the foreignKey id to another Field - Using Django
I want the table in database save the name and not the id of the foreignKey. And this is the model.py from pyexpat...
Read more >
Display name instead of ID for relation - UI Bakery Docs
For ease of use, we would like to display Customer Name values in the Orders table instead of their Customer Number, which is...
Read more >
Foreign Key Constraint | CockroachDB Docs
The name of the column the foreign key references. If you do not include the ref_column_name you want to reference from the parent_table...
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