Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

sql - Use Django ORM to take values from field that have some Set of values in enother field

How to get bellow by using django ORM

myset = {2, 3}

I need names from field 'name' that have '2'&'3' only in 'value' field:

class Mytable(models.Model):
    name = models.CharField(max_length=45, blank=True, null=True)
    value = models.CharField(max_length=45, blank=True, null=True)

Table in DB (model.Mytable)

'name'|'value'
--------------
'Ann' |'2'
'Ann' |'3'
'Ann' |'5'
'John'|'2'
'John'|'3'
'Jim' |'3'
'Jim' |'2'
'Pit' |'7'
'Pit' |'8'

Needed output: ['John', 'Jim'] # value '2','3' only

So, I try to get construction like:

QuerySet [('Ann', ['2','3','5']), ('John', ['2', '3']), ... 

using .annotatte(), .values()

But i don't know how get it using Django ORM.

Or i can use some filter in .filters()?

question from:https://stackoverflow.com/questions/66049246/use-django-orm-to-take-values-from-field-that-have-some-set-of-values-in-enother

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...