V
V
Viktor Usachov2017-03-02 12:35:14
Django
Viktor Usachov, 2017-03-02 12:35:14

What is the correct way to describe ManyToManyField in Django 1.10?

Good afternoon, comrades.
I have a django 1.3 (python 2.7) project that I successfully upgraded to django 1.10 (python 3.4).
The problem only occurred with two models linked as ManyToMany, other similar models still work. Probably, I did not take into account some changes in the ORM. The code:

class Dataset(HistoryModel):
    """
    Класс для отчета по расходу ресурсов. Содержит редактируемую структуру полей.
    """

    name = models.CharField('Название набора данных', max_length=300)
    fields = models.ManyToManyField(DatasetField, verbose_name='Поля набора данных', db_table="data_proxy_fields")
    
    class Meta:
        verbose_name = 'Набор данных'
        ordering = ['name']
        db_table = "data_proxy"


class DatasetField(HistoryModel):
    """
    Поле набора данных.
    """
    name = models.CharField('Пользовательское название', max_length=300)
    model_content_type = models.ForeignKey(ContentType)
    index = models.IntegerField('Индекс поля', default=0)

    class Meta:
        verbose_name = "Поле набора данных"
        ordering = ['model_content_type__name', 'index']
        db_table = "dataset_field"

Where did the error occur:
dataset = Dataset.objects.get(id=dataset_id)
return serializers.serialize("json", dataset.fields.all())

Traceback (most recent call last):
  File "/WORK/daily/project/project/dataset/views.py", line 451, in get_all_fields
    return serializers.serialize("json", dataset.fields.all())
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/core/serializers/__init__.py", line 129, in serialize
    s.serialize(queryset, **options)
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/core/serializers/base.py", line 79, in serialize
    for count, obj in enumerate(queryset, start=1):
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/query.py", line 256, in __iter__
    self._fetch_all()
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 824, in execute_sql
    sql, params = self.as_sql()
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 369, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 47, in pre_sql_setup
    order_by = self.get_order_by()
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 293, in get_order_by
    field, self.query.get_meta(), default_order=asc))
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 553, in find_ordering_name
    field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 586, in _setup_joins
    pieces, opts, alias)
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1402, in setup_joins
    names, opts, allow_many, fail_on_missing=True)
  File "/WORK/envs/dj1.10/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1327, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'name' into field. Choices are: app_label, datasetfield, id, logentry, model, permission

I think that something is wrong in the code of the models themselves, but it was not possible to find out what exactly.
I tried to debug, but I go into the depths of the orm, it is unlikely that the error is there.
I'm asking for help, since I've been struggling with the code for 3 days and combing stackoverflow - to no avail.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Usachev, 2017-03-06
@victor_usachev

I just killed a lot of time due to the lack of information in the log and my own inattention. I searched for the problem wherever possible, but everything turned out to be simple - the field specified in the ordering does not exist:

class DatasetField(HistoryModel):
    # ...
    model_content_type = models.ForeignKey(ContentType)
    class Meta:
        # ...
        ordering = ['model_content_type__name', 'index']

django.contrib.contenttypes.models.ContentType used to have a name field , but it has been removed.

B
Bjornie, 2017-04-04
@Bjornie

The error at the very end says that the field was not found in the table. And asks (suggests) which of these fields can be correct: app_label, datasetfield, id, logentry, model, permission

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question