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?
This section of the docs may help:
When defining ModelResource fields it is possible to follow model relationships:
class BookResource(resources.ModelResource):
class Meta:
model = Book
fields = ('author__name',)
So likely in your case the fields definition would be:
fields = (user_departament__name, ...)
Thanks @matthewhegarty !
@cesardft if you feel that this doesnt solve your issue, please let us know. Im closing this in the meantime.
Hi @matthewhegarty
Thanks for the reply. Apparently this is a good solution, but it doesn't seem to have worked for me. I will share my code with you. Did I do it right? I'm a little beginner in programming.
My field Departament is:
class Departament(models.Model):
departament_name = models.CharField(max_length=50, blank=False, verbose_name='Departamento')
And User is:
class User(models.Model):
user_departament = models.ForeignKey('Departament', on_delete=models.PROTECT, null=False, blank=True, verbose_name='Departamento')
So i need to put this in the 'field' var?
field = ('user_departament__departament_name', ...) ?
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:
This will save you many hours in trying to fix issues.
It works! Thank you very much and sorry for the inconvenience.