djongo icon indicating copy to clipboard operation
djongo copied to clipboard

ArrayReferenceField does not populate at query, is not a list but a set

Open wawel37 opened this issue 4 years ago • 1 comments

ArrayReferenceField returns set of IDs instead of populated list of objects as it's said in the docs "Upon retrieving an entry from the DB the corresponding authors are automatically looked up and the author list is populated"

Python script

=== Our models ===

class TextElement(models.Model):
    name = models.CharField(max_length=50)
    
    class Meta:
        abstract = True
    
class TextForm(forms.ModelForm):
    class Meta:
        model = TextElement
        fields = (
            'name',
        )

class Dish(models.Model):
    name = models.CharField(max_length=40)
    author = models.CharField(max_length=40)
    types = models.ArrayField( 
        model_container=TextElement,
        model_form_class=TextForm
    )
    recipeID = models.IntegerField()
    reviews = models.ArrayReferenceField(
        to=Review,
        on_delete=models.CASCADE,
    )
    img = models.CharField(max_length=100)

class Review(models.Model):
    text = models.CharField(max_length=1000)
    userName = models.CharField(max_length=50)
    rating = models.IntegerField()

=== Our code ===

review = Review(text= "text", userName="userName", rating = 5)
dish = Dish(name= 'testDish', author= 'testAuthor', types= [{'name': 'testType1'}, {'name': 'testType2'}], recipeID= 2, img= 'testImg')
dish.save()
dish.reviews.add(review)

print("reviews_id query: ")
for q in Dish.objects.filter().values():
    print(q['reviews_id'])

print("reviews query: ")
for q in Dish.objects.filter().values():
    print('reviews' in q)

Traceback

Traceback

reviews_id query: {37} {38} set() set() set() set() {39} {42} {43} set() set() {46} {47} set() set() set() {51} {52} {53} {54} {55} {56} {57} {58} {59} {60} {61} {62} {63} {64} {65} {66} {67} {68} {69} {70} {71} {72} {73} reviews query: False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False

What is strange, the 'reviews' field is replaced by 'reviews_id'. The migrations are up to date.

We are fully aware how awful it all looks, its just because we are playing with it and testing things out. We couldn't find anything else than this simple and short documentation, so we are opening an issue.

wawel37 avatar May 26 '21 19:05 wawel37

I have the same problem my friend

Akatroj avatar Jan 23 '23 20:01 Akatroj