I have a choice tuple like that
CATEGORY_CHOICES = (
(DB_ENUMS.GENERAL.EMPTY, 'All'),
('Fashion and style',
(
(DB_ENUMS.CATEGORY.FASHION_AND_STYLE, 'All Fashion and Style'),
('Clothes/Shoes',
(
(1, 'string')
)
)
)
)
)
But this one
((1, 'string'))
is shown on the same level as
'All Fashion and Style'
Am I trying to use forbidden things? I found nothing about limits in documentation.
By default, Django only supports 2 levels of hierarchy in a choice field:
MEDIA_CHOICES = (
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
)
However, there are some third-party apps like django-categories that provide the ability to have a hierarchy of choices.
Related
Environments
Django 2.0
DRF 3.11
mariaDB
I want to output'N' if at least one of the columns is 0, and'Y' if all of them are 1.
Database
Model
class UserPoint:
user = models.ForeignKey('User', others options)
poinit = models.IntegerField()
used = models.SmallIntegerField(default=0)
deleted = models.SmallIntegerField(default=0)
Expected results
total_point_used : Sum of points where used is 1
used_status : 'N' if any of used has 0 or 'Y' if all values of used are 1
user
username
total_point_used
used_status
1
'AA'
600
'N'
2
'BB'
0
'N'
3
'CC'
500
'Y'
Tried queryset
q = UserPoint.objects.filter(
deleted=0
).values(
username=F('user__username')
.... others values ....
).annotate(
total_point_used=Sum( # Sum of used point
Case(
When(
used=1,
then='point',
), output_field=IntegerField(),
)
),
# I don't know what to do with used_status.
# tried <- not work
#used_status=Case(
# When(
# used=1,
# then=Value('Y')
# ),
# default=Value('N'),
# output_field=CharField()
# )
# I want something like this.
used_status=Exists(used=0, then='N', default='Y')
)
How can I get the value I expect in annotate?
Probably you can use Sum with filter:
from django.db.models import Q, Case, When, Sum
# rest of the code
.annotate(
total_point_used=Sum('point', filter=Q(used=1)),
used_status=Case(
When(
used=1,
then=Value('Y')
),
default=Value('N'),
output_field=CharField()
)
)
I have created and tested a rule in the powerbi app that works fine in "view as", but when I publish my dataset with the rule, the report in the service is not working.
I am not sure if it has anything to do with that I am an admin on the project and using my own email to test out the filtering (though this isn't a problem in the app). Any thoughts? I realize this might be quite vague. My organization is still rather new to PBI.
Var CompanyPCC = CALCULATETABLE (
VALUES ( 'User Access'[PCC]),
FILTER ( 'User Access', 'User Access'[Login] = USERPRINCIPALNAME() )
)
RETURN
OR(
"ALL" IN CompanyPCC,
PATHCONTAINS(CompanyPCC,'Company'[Company PCC])
)
&&
Var Country= CALCULATETABLE (
VALUES ( 'User Access'[Country] ),
FILTER ( 'User Access', 'User Access'[Login] = USERPRINCIPALNAME() )
)
RETURN
OR(
"ALL" IN Country,
PATHCONTAINS(Country,'Company'[Company Profile Country])
)
&&
Var Brand= CALCULATETABLE (
VALUES ( 'User Access'[Brand] ),
FILTER ( 'User Access', 'User Access'[Login] = USERPRINCIPALNAME() )
)
RETURN
OR(
"ALL" IN Brand,
PATHCONTAINS(Brand,'Company'[Company Brand])
)
For RLS to work, a user needs to have read only permission in the workspace. Members or admins see everything by default. See:
https://learn.microsoft.com/en-us/power-bi/admin/service-admin-rls#using-rls-with-workspaces-in-power-bi
I want a listing to be able to have a specific category, so the seller will choose a general category and then onto a more specific one, for example:
Men's Wear:
Jeans:
Skinny Fit
Slim Fit
Regular Fit
Pants:
Slim Fit
Formal
Casual
Women's Apparel:
Tops:
Tunics
Denim:
Jeans
I tried the named groups like in django docs:
https://docs.djangoproject.com/en/3.0/ref/models/fields/#field-choices-named-groups
MEDIA_CHOICES = [
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
]
But when I tried, it gives the error
product.Listing.category: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
here is my code:
class Listing(models.Model):
CATEGORY = [
('Women\'s Apparel', (
('Dresses', (
('party_dresses', 'Party Dresses'),
)
),
),
),
]
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
description = models.CharField(max_length=255)
new_condition = models.BooleanField()
category = models.CharField(choices=CATEGORY, max_length=255)
why is it showing error ? is it because the enum groups cant be nested more than 1 level ?
thank you
Let's say I'm making a media library model, and there are these choices in named groups:
models.py
class MediaLibrary(models.Model):
MEDIA_CHOICES = [
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
)
]
media_type = models.CharField(max_length=50, choices=Media_Choices)
If I make a request through DRF, it would return all the objects which match either "Audio" and "Video".
How can I filter the request so that it only returns objects under the "Audio" group. (Or the "Video" group.)
create a model manger and do filter as follows
class MediaManager(models.Manager):
def video_items(self):
return self.get_queryset().filter(media_type='Video')
---your models here--
objects = MediaManager()
then you can call in your views as MediaLibrary.objects.video_items(), similarly filer audio too.
I have a WorderOrder class that has predefined work order types:
class WorkOrder( models.Model ) :
WORK_TYPE_CHOICES = (
( 'hc', 'Heating and cooling' ),
( 'el', 'Electrical' ),
( 'pl', 'Plumbing' ),
( 'ap', 'Appliances' ),
( 'pe', 'Pests' ),
( 'ex', 'Exterior' ),
( 'in', 'Interior' ),
( 'ot', 'Others' ),
)
work_type = models.CharField( max_length = 2, choices = WORK_TYPE_CHOICES )
vendor = models.ForeignKey( Vendor, null = True, blank = True )
Therefore each order must have one work order type. Later down the road, a vendor can also be assigned to a work order.
I want the Vendor class to have a M2M relationship to the same work order choices in the WorkOrder class. In other words, each vendor are able to do one or many work types. For example, Bob's Plumbing can only do "Plumbing", whereas Solid Home Repair can do "Electrical", "Plumbing", "Exterior", and "Interior".
I understand I can create another table called WorkType and use foreign keys from WorkOrder and a M2M from Vendor, but since I feel I won't be changing the work type choices, I would rather have them predefined in models.py.
Also if I can predefine it in models.py, then I don't have to pre-populate the table WorkType during deployments and upgrades.
Some options for you:
create a model for work_type_choices, instantiate the records (hc, el, etc), then use a manytomany field, or
create a charfield and save CSV values to it (eg: "hc, el"), spliting/joining the value into it's elements as required, or
encapsule the above charfield and functions into a custom field and use that
leverage someone else's snippet, eg:
http://djangosnippets.org/snippets/1200/