in a form i’m trying to let the user select an item from a dropdown (select). (this works and it is save in the database)
When a user needs to edit this an other form is used. (see the codeblock below)
In this form the user got the exact same layout as form above but now comes the tricky part.
When I created that form it only shows the selected option in the dropdown. but i need the dropdown to contain all the option the user could choose from in the first form. plus as an added bonus when this form is created the initial value need to be the previous selected value.
So I tought I could combine 2 querysets. but this doesn’t work.
Error i’m getting.
Cannot combine queries on two different base models.
I see this error and I can understand why this wouldn’t be happening. there is now key or anything and django basicly creates the following SQL statement.
select
*
from storeassortment where storetype = 'butcher'
union
select
*
from playerownedstores where shelfid = 1 and slugstore = VAR1
NOW my Question.
Is it possible to get the following sql statement?
the querysets consits of 2 different models that are not related to eachother in anyway.
in SQL I would sort of do it this way:
select
assortiment
from storeassortment where storetype = 'butcher'
union
select
ass_1_type
from playerownedstores where shelfid = 1 and slugstore = VAR1
Forms.py snippet:
class Update_Assortiment(forms.Form):
def __init__(self, *args, **kwargs):
self.storeslug = kwargs.pop('storeslug', None)
self.storetype = kwargs.pop('storetype', None)
# print(self.storeslug)
super().__init__(*args, **kwargs)
added_quary = models.StoreAssortment.objects.filter(Storetype=self.storetype)
initial_quary = models.POSassortiment.objects.filter(storeslug=self.storeslug,shelfID=1)
records = (initial_quary | added_quary).distinct()
self.fields['update1'].queryset = records
shelf1 = forms.CharField(required=False,label='Shelf',initial=1,max_length=2,disabled=True, widget=forms.TextInput(attrs={"class":"formstorelevel","readonly":True}))
update1 = forms.ModelChoiceField(queryset=models.POSassortiment.objects.none(),required=False, label='',empty_label=None,widget=forms.Select(attrs={"class":"formassortimenttable"}))
The querysets that are returned seperatly:
added_quary
<QuerySet [<StoreAssortment: Sausage>, <StoreAssortment: Dry aged beef>, <StoreAssortment: Schnitzel>, <StoreAssortment: Meatball>, <StoreAssortment: Pork>]>
initial_quary
<QuerySet [<POSassortiment: Dry aged beef>]>
models.py snippet
class POSassortiment(models.Model):
storeslug = models.ForeignKey(PlayerOwnedStores, on_delete=models.CASCADE, null=True, blank=True)
storetype = models.CharField(max_length=75)
storelevel = models.PositiveSmallIntegerField(default=0, validators=[MaxValueValidator(5)])
shelfID = models.CharField(max_length=3,choices=keuze.SHELFS)
ass_1_type = models.CharField(max_length=75,choices=keuze.SHOPCHOICES,blank=True,null=True)
ass_1_amount = models.IntegerField(default=0,blank=True,null=True)
ass_1_price = models.FloatField(default=0,blank=True,null=True)
def __str__(self):
return str(self.ass_1_type)
class StoreAssortment(models.Model):
Storetype = models.CharField(max_length=75,choices=keuze.STORES)
Assortiment = models.CharField(max_length=75,choices=keuze.SHOPCHOICES,blank=True)
popularity = models.PositiveSmallIntegerField(default=50,validators=[MaxValueValidator(100)])
def __str__(self):
return self.Assortiment