1

database in MongoDB (djongo)

I have registered a model in admin.py

admin.site.register(Media)

models.py

class Media(BaseModel):
    _id = models.ObjectIdField(primary_key=True)
    url = models.URLField()
    media_type = models.CharField(max_length=100)
    user = models.ForeignKey(User, db_column="user", on_delete=models.CASCADE)
    post = models.ForeignKey(Post, db_column="post", on_delete=models.CASCADE)
    group = models.ForeignKey(Group, db_column="group", on_delete=models.CASCADE)

    class Meta:
        db_table = "media"

while changing values using the admin site I got these errors. enter image description here

can you help me to solve this error?

django==3.0.5

djongo==1.3.4

  • Hello harshil Please check this stackoverflow case [link](https://stackoverflow.com/questions/48041375/django-select-a-valid-choice-that-choice-is-not-one-of-the-available-choices) – Hiren30598 May 14 '21 at 04:48
  • I didn't use model forms in admin, I have worked with django admin many times but I think this error's cause is mongodb(djonog) @Hiren30598 – harshil suthar May 14 '21 at 05:35

1 Answers1

0

I noticed that when you try to save an instance of an object with PK an ObjectId that it is transformed into a string and consequently no longer corresponds to an instance of an object, so with the override of the get_form method in POST you can intercept this data and change the string to ObjectId, but as you can see in the [Django documentation][1]:

The QueryDicts at request.POST and request.GET will be immutable when accessed in a normal request/response cycle.

so you can use the recommendation from the same documentation:

To get a mutable version you need to use QueryDict.copy()

or ... use a little trick, for example, if you need to keep a reference to an object for some reason or leave the object the same:

# remember old state
_mutable = data._mutable

# set to mutable
data._mutable = True

# сhange the values you want
data['param_name'] = 'new value'

# set mutable flag back
data._mutable = _mutable

where data it is your QueryDicts

In this case:

@admin.register(MyMode)
class MyModelAdmin(admin.ModelAdmin):
    list_display = ('....')
    ....

    def get_form(self, request, obj, change, **kwargs):
        if request.POST:
            # remember old state
            _mutable = request.POST._mutable
            # set to mutable
            request.POST._mutable = True
            # сhange the values you want
            request.POST['user'] = ObjectId(request.POST['user'])
            request.POST['post'] = ObjectId(request.POST['post'])
            request.POST['group'] = ObjectId(request.POST['group'])
            # set mutable flag back
            request.POST._mutable = _mutable

        return super().get_form(request, obj=obj, change=change, **kwargs)
zN3utr4l
  • 21
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 03 '21 at 18:56
  • Please add some explanation to your answer such that others can learn from it – Nico Haase Dec 05 '21 at 12:19
  • Post edited with explanation – zN3utr4l Dec 06 '21 at 07:34