2

I'm facing a strange problem maybe related with some cache that I cannot find.

I have the following Models:

class Incubadores(models.Model):
    incubador = models.CharField(max_length=10, primary_key=True)
    posicion = models.CharField(max_length=10)

class Tareas(TimeStampedModel):
    priority = models.CharField(max_length=20, choices=PRIORITIES, default='normal')
    incubador = models.ForeignKey(Incubadores, on_delete=models.CASCADE, null=True, db_column='incubador')
    info = JSONField(null=True)
    datos = JSONField(null=True)

    class Meta:
        ordering = ('priority','modified','created')

I previously didn't have the argument db_column, so the Postgres column for that field was incubador_id I used the argument db_column to change the name of the column, and then I run python manage.py makemgrations and python manage.py migrate, but I'm still getting the column as incubadores_id whenever I perform a query such as:

>>> tareas = Tareas.objects.all().values()
>>> print(tareas)
<QuerySet [{'info': None, 'modified': datetime.datetime(2019, 11, 1, 15, 24, 58, 743803, tzinfo=<UTC>), 'created': datetime.datetime(2019, 11, 1, 15, 24, 58, 743803, tzinfo=<UTC>), 'datos': None, 'priority': 'normal', 'incubador_id': 'I1.1', 'id': 24}, {'info': None, 'modified': datetime.datetime(2019, 11, 1, 15, 25, 25, 49950, tzinfo=<UTC>), 'created': datetime.datetime(2019, 11, 1, 15, 25, 25, 49950, tzinfo=<UTC>), 'datos': None, 'priority': 'normal', 'incubador_id': 'I1.1', 'id': 25}]>

I need to modify this column name because I'm having other issues with Serializers. So the change is necessary.

  • If I perform the same query in other Models where I've also changed the name of the default field. The problem is exactly the same.
  • It happens both on the shell and on the code.
  • I've tried with different queries, to make sure it's not related to Django lazy query system, but the problem is the same. I've also tried executing django.db.connection.close().
  • If I do a direct SQL query to PostgreSQL, it cannot find incubador_id, but only incubador, which is correct.

Anyone has any idea of what can be happening? I've already been 2 days with this problem and I cannot find a reason :( It's a very basic operation.

Thanks!

3
  • Your code is running fine. Django append _id itself for all foreign keys. If your have foreign key with name abc, in your database it will be saved as abc_id. abc will contain complete object of that foreign key. Commented Nov 7, 2019 at 7:40
  • You can try like this Tareas.objects.all().values('id', 'incubador'), then you can understand that it is running fine. Commented Nov 7, 2019 at 7:41
  • @MuhammadHassan, my original code includes .values() function, but I simplified the code in the example because it was happening anyway. The argument db_column removes the _id added by Django. Commented Nov 7, 2019 at 11:31

1 Answer 1

1

This answer will explain why this is happening.

Django's built-in serializers don't have this issue, but probably won't yield exactly what you're looking for:

>>> from django.core import serializers
>>> serializers.serialize("json", Tareas.objects.all())
'[{"model": "inc.tareas", "pk": 1, "fields": {"priority": "normal", "incubador": "test-i"}}]'

You could use the fields attribute here, which seems like it would give you what you're looking for.

You don't specify what your "other issues with Serializers" are, but my suggestion would be to write custom serialization code. Relying on something like .values() or even serializers.serialize() is a bit too implicit for me; writing explicit serialization code makes it less likely you'll accidentally break a contract with a consumer of your serialized data if this model changes.

Note: Please try to make the example you provide minimal and reproducible. I removed some fields to make this work with stock Django, which is why the serialized value is missing fields; the _id issue was still present without the third-party apps you're using, and was resolved with serializers. This also isn't specific to PG; it happens in sqlite as well.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!! That was the reason, I was getting crazy.
So about the Serializers. I use DRF ones, and I'm having an error that I was trying to track down this way. But I think I'm finding the reason. Otherwise I'll post it too in a different thread.
Yeah, it looks like their serializers should be a good, explicit way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.