Import name instead of ID in a foreign key field
See original GitHub issueHi!
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:
- Created 3 years ago
- Comments:9 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?That is almost correct. It must be
fields
notfield
. 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:
This will save you many hours in trying to fix issues.