Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

something wrong with django_mongoengine/forms/document_options.py #151

Open
BradyHu opened this issue Jun 24, 2021 · 4 comments
Open

something wrong with django_mongoengine/forms/document_options.py #151

BradyHu opened this issue Jun 24, 2021 · 4 comments

Comments

@BradyHu
Copy link

BradyHu commented Jun 24, 2021

If the name of the reference field is different from the name of the reference, it will cause a KeyError.

    def get_field_by_name(self, name):
        """
        docs
        """
        try:
            try:
                return self._field_cache[name]
            except TypeError:
                self._init_field_cache()
                return self._field_cache[name]
        except KeyError:
            raise FieldDoesNotExist('%s has no field named %r'
                                    % (self.object_name, name))
    def _init_field_cache(self):
        if self._field_cache is None:
            self._field_cache = {}

        for f in self.document._fields.values():
            if isinstance(f, ReferenceField):
                document = f.document_type
                self._field_cache[document._meta.module_name] = (f, document, False, False)
            else:
                self._field_cache[f.name] = (f, None, True, False)

        return self._field_cache
@BradyHu
Copy link
Author

BradyHu commented Jun 24, 2021

change self._field_cache[document._meta.module_name] to self._field_cache[f.name] maybe will work,I guess

@last-partizan
Copy link
Contributor

Hi.

Would you try to fix this issue?

You need to add test case exposing issue, and then try your fix and create PR.

@BradyHu
Copy link
Author

BradyHu commented Jun 30, 2021

I know when it happened now:
when I use DRF and serializers.PrimaryKeyFIeld, It happened, while ,I dont know how to fix it....

class SceneBIReport(MongoCoreModel):
    stats_field = fields.ReferenceField(DataField, reverse_delete_rule=mongoengine.NULLIFY,
                                        blank=True, null=True)

class SceneBIReportSerializer(m_serializers.DocumentSerializer):
    stats_field = serializers.PrimaryKeyRelatedField(
        queryset=models.DataField.objects.all(),
        required=False,
        allow_null=True,
        allow_empty=True,
    )

and when run validate , problem happened....

@last-partizan
Copy link
Contributor

You suggested changing something in _init_field_cache, so you could start to creating test case for this issue.

DRF is probably unrelated, take a look at validation, find out what is being called. Then create test for it and reproduce bug.

Then, try your suggestion:

change self._field_cache[document._meta.module_name] to self._field_cache[f.name] maybe will work,I guess

If it works, create PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants