django-queryset

Django ORM - percent sign for like

让人想犯罪 __ 提交于 2019-12-10 14:53:16
问题 On my site user should have an ability to filter numbers, like *123*321* , that will match "666 123 555 321 111" or LIKE '%123%321%' . By default django's orm escapes %-sign. I can use regex, or raw query, but is there some workaround? UPD: i'll place it here for displaying another way. integer_search = [] # for colorizing found substrings if actual['integer']: integer_match = filter(None, actual['international'].split('*')) integer_search = integer_match integer_match = ''.join('%s[[:digit:]

django how to get count for manytomany field

谁说胖子不能爱 提交于 2019-12-10 14:47:42
问题 I have model for question: class Question(models.Model): user = models.ForeignKey(User) title = models.CharField(max_length=120) description = models.TextField() answers = models.ManyToManyField('Answer',related_name='answer_name', blank=True) post_date = models.DateTimeField(auto_now=True) def __unicode__(self): return self.title And I have model for answer: class Answer(models.Model): user = models.ForeignKey(User) question = models.ForeignKey(Question) ans_body = models.TextField() post

Django model latest() method

穿精又带淫゛_ 提交于 2019-12-10 13:23:37
问题 I am having the following issue (BTW I think I had not had this problem the day before): >>> rule = Rule.objects.get(user=user) >>> rule.id 1 >>> rule = Rule.objects.get(user=user).latest('id') AttributeError: 'Rule' object has no attribute 'latest' Why am I getting the error? 回答1: The get() function of the Model Manager returns an instance of the Model itself. The latest() function you mention belongs to the QuerySet class. Calling .filter(), .all(), .exclude() etc, all return a QuerySet.

Annotating SUM aggregation function leading to 'None' value in Django

主宰稳场 提交于 2019-12-10 12:55:12
问题 Doing my first real Django project, and need guidance. Background: My project is a reddit clone. Users submit links+text. Visitors upvote or downvote. There's a social_ranking algo, runs every ~2 mins as a background script, reranks all the submissions according to net votes and freshness of content. Fairly vanilla stuff. Problem: Ordering by votes isn't working correctly, because votes are being initialized as None instead of 0 . This causes submissions with None votes to rank below

How to Query in django ORM for foreign key fields

流过昼夜 提交于 2019-12-10 12:31:09
问题 class Company(models.Model): name = models.CharField(max_length=60) class Employee(models.Model): dept = models.ForeignKey(Company) Django ORM: Here I want to access name through Employee class in Django ORM. I wrote something like this: `Employee.objects.filter(name = dept__Company)`(Used two double underscore for other model class) Will above is correct? Can someone have any idea? 回答1: From what I understand, you're just trying to retrieve the employees that belong to a certain company. To

Django Advanced Filtering

拟墨画扇 提交于 2019-12-10 11:36:32
问题 Can I filter a queryset based on a model field's choices? model: COLORS = ( ('BLACK', 'black'), ('RED', 'red'), ('BLUE', 'blue'), //etc.. ) class Thing(Models.model): color = models.CharField(max_length=5, choices=COLORS) view: def filter_by_color(request): q = Thing.objects.filter(???) Is there a way to filter Thing based on different color choices? Also, is there a way to write this dynamically, so that all color choices can respond to a single view? 回答1: Do you want this? view def filter

Django: form to query database

别说谁变了你拦得住时间么 提交于 2019-12-10 11:35:51
问题 I want a user to be able to perform the following query: Fetch all the people, without Phd, with full time contract with a contract within two dates. that translates in Django: Contract.objects.filter( person__is_doctor = False, type_contract = 'full', starting_date__gte = start_date, ending_date__lte = end_date ) How can I make a form/view/template to allow the user enter both start_date and end_date and show the results? models class Person(models.Model): name = models.CharField(max_length

Django queryset specific order by based on values of foreign key

喜欢而已 提交于 2019-12-10 10:48:52
问题 I have two models, Position and Player, for a baseball site. Positions are named pitcher, catcher, first base, second base, third base, etc. class Position(models.Model): name = models.CharField(max_length=100) slug = models.SlugField() class Player(models.Model): name = models.CharField(max_length=300) slug = models.SlugField() position = models.ForeignKey(Position) Is there a way to make one query to return players in a specific order? For example, I'd like to do something like: Player

django distinct and order_by

被刻印的时光 ゝ 提交于 2019-12-10 10:26:18
问题 Foo.objects.all().distinct('foo') won't remove duplicates Foo.objects.all().order_by().distinct('foo') does remove them. I have Meta.ordering = ('-field1', '-field2) Does Foo.objects.all().order_by().distinct('foo') respects the Meta.ordering? i.e. I want distinct values ordered by Meta.ordering and not sure how to do it. Django Documentation is confusing and don't address how order_by() without any argument works. I'm using postgresql 9.3 btw. In [28]: BestMomsdiaryThread.objects.all()

Is there an OR filter? - Django

核能气质少年 提交于 2019-12-10 09:56:50
问题 is there any way of doing the following Unicorn.objects.or_filter(magical=True).or_filter(unicorn_length=15).or_filter(skin_color='White').or_filter(skin_color='Blue') where or_filter stands for an isolated match I remember using something similar but cannot find the function anymore! Help would be great! Thanks :) 回答1: You're looking for Q objects. 来源: https://stackoverflow.com/questions/2964540/is-there-an-or-filter-django