'Django ORM returns non-iterable object
The model is
class WeeklyStrMst(BaseModel):
StoreId = models.AutoField(primary_key=True)
TDLinx_No = models.IntegerField(blank=True, null=True, default=None)
Week = models.IntegerField(blank=True, null=True, default=None)
Week_Number = models.IntegerField(blank=True, null=True, default=None)
Year = models.IntegerField(blank=True, null=True, default=None)
Category = models.CharField(max_length=75, blank=True, null=True, default=None)
SeasonalPackaging = models.CharField(max_length=75, blank=True, null=True, default=None)
Consumption = models.CharField(max_length=75, blank=True, null=True, default=None)
Manufacturer = models.CharField(max_length=75, blank=True, null=True, default=None)
PackType = models.CharField(max_length=75, blank=True, null=True, default=None)
Volume = models.FloatField(blank=True, null=True, default=None)
POS = models.FloatField(blank=True, null=True, default=None)
StoreName = models.CharField(max_length=75, blank=True, null=True, default=None)
MasterChain = models.CharField(max_length=75, blank=True, null=True, default=None)
TerritoryName = models.CharField(max_length=75, blank=True, null=True, default=None)
RegionName = models.CharField(max_length=75, blank=True, null=True, default=None)
StoreClassification = models.CharField(max_length=75, blank=True, null=True, default=None)
StateName = models.CharField(max_length=75, blank=True, null=True, default=None)
class Meta:
managed = True
db_table = config_data[settings.REGION_NAME]['tables']['weekly_mstr']
WeeklyStrMst.objects.filter(StoreId__in=stores).values()
Here stores is of list datatype containing 6000-30000 values which are non-null, integer, so when I execute this, it returns me -
<django.db.models.query.QuerySet object at 0x0000024FD4EAAAF0>
I have tested with less than 2000 values it works fine. What could be possible reason and workaround for this scenario? How to pass a large list of array as a filter criteria?
Solution 1:[1]
Thank you all, I had to update the library mssql-django, the earlier version had limit on the number of parameters that can be passed, which they have updated.
Solution 2:[2]
The QuerySet class is iterable - you can use it inside any for
loop (even in templates).
The values function returns a QuerySet of dictionaries instead of your model instances and is, rest assured, also an iterable.
Edit -
If you're using SQLite for your database, there is usually a limit set on how many values you can pass for IN
queries - https://sqlite.org/limits.html. You can change this value depending on the system you're using. Besides, if you're dealing with extremely heavy datasets, you'd be better off with a more scalable database like Postgres or MySQL.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Debadatta Bhattacharjee |
Solution 2 |