django-orm

django-orm case-insensitive order by

丶灬走出姿态 提交于 2019-11-26 18:13:40
问题 I know, I can run a case insensitive search from DJango ORM. Like, User.objects.filter(first_name__contains="jake") User.objects.filter(first_name__contains="sulley") User.objects.filter(first_name__icontains="Jake") User.objects.filter(first_name__icontains="Sulley") And also, I can fetch them as user_list = User.objects.all().order_by("first_name") # sequence: (Jake, Sulley, jake, sulley) user_list = User.objects.all().order_by("-first_name") # for reverse # sequence: (sulley, jake, Sulley,

Django: implementing JOIN using Django ORM?

懵懂的女人 提交于 2019-11-26 17:45:19
问题 I have a Q&A type of site built in Django with the following models: class Question(models.Model): title = models.CharField(max_length=70) details = models.TextField() class Answer(models.Model): question_id = IntegerField() details = models.TextField() I need to display a specific question together with its answers. Normally I'd need 2 queries to do that: Question.objects.get(id=1) Answer.objects.get(question_id=1)[:10] I'm hoping to retrieve everything using one query. In MySQL it'd be:

Django orm get latest for each group

怎甘沉沦 提交于 2019-11-26 17:28:28
问题 I am using Django 1.6 with Mysql. I have these models: class Student(models.Model): username = models.CharField(max_length=200, unique = True) class Score(models.Model) student = models.ForeignKey(Student) date = models.DateTimeField() score = models.IntegerField() I want to get the latest score record for each student. I have tried: Score.objects.values('student').annotate(latest_date=Max('date')) and: Score.objects.values('student__username').annotate(latest_date=Max('date')) as described

Default image for ImageField in Django's ORM

大城市里の小女人 提交于 2019-11-26 16:33:19
问题 I'm using an ImageField to store profile pictures on my model. How do I set it to return a default image if no image is defined? 回答1: I haven't tried this, but I'm relatively sure you can just set it as a default in your field. pic = models.ImageField(upload_to='blah', default='path/to/my/default/image.jpg') EDIT : Stupid StackOverflow won't let me comment on other people's answers, but that old snippet is not what you want. I highly recommend django-imagekit because it does tons of great

Django queryset get exact manytomany lookup [duplicate]

限于喜欢 提交于 2019-11-26 16:10:38
问题 This question already has answers here : How to do many-to-many Django query to find book with 2 given authors? (4 answers) Closed 6 years ago . I have a pk list of instances of Tag model, say pk_list = [10, 6, 3] I have another model with m2m field of tags and an instance that contains exactly 3 tags (of above pks). class Node(models.Model): ... tags = models.ManyToManyField(Tag, related_name='nodes') I'd like to retrieve a Node that contains exact set of tags as specified in my pk_list.

LEFT JOIN Django ORM

≯℡__Kan透↙ 提交于 2019-11-26 15:49:02
问题 I have the following models: class Volunteer(models.Model): first_name = models.CharField(max_length=50L) last_name = models.CharField(max_length=50L) email = models.CharField(max_length=50L) gender = models.CharField(max_length=1, choices=GENDER_CHOICES) class Department(models.Model): name = models.CharField(max_length=50L, unique=True) overseer = models.ForeignKey(Volunteer, blank=True, null=True) location = models.CharField(max_length=100L, null=True) class DepartmentVolunteer(models

Django Blob Model Field

試著忘記壹切 提交于 2019-11-26 15:35:39
问题 How do you store a "blob" of binary data using Django's ORM, with a PostgreSQL backend? Yes, I know Django frowns upon that sort of thing, and yes, I know they prefer you use the ImageField or FileField for that, but suffice it to say, that's impractical for my application. I've tried hacking it by using a TextField, but I get occassional errors when my binary data doesn't strictly confirm to the models encoding type, which is unicode by default. e.g. psycopg2.DataError: invalid byte sequence

Optimizing database queries in Django REST framework

拈花ヽ惹草 提交于 2019-11-26 15:16:43
I have the following models: class User(models.Model): name = models.Charfield() email = models.EmailField() class Friendship(models.Model): from_friend = models.ForeignKey(User) to_friend = models.ForeignKey(User) And those models are used in the following view and serializer: class GetAllUsers(generics.ListAPIView): authentication_classes = (SessionAuthentication, TokenAuthentication) permission_classes = (permissions.IsAuthenticated,) serializer_class = GetAllUsersSerializer model = User def get_queryset(self): return User.objects.all() class GetAllUsersSerializer(serializers

Django select only rows with duplicate field values

こ雲淡風輕ζ 提交于 2019-11-26 15:12:22
问题 suppose we have a model in django defined as follows: class Literal: name = models.CharField(...) ... Name field is not unique, and thus can have duplicate values. I need to accomplish the following task: Select all rows from the model that have at least one duplicate value of the name field. I know how to do it using plain SQL (may be not the best solution): select * from literal where name IN ( select name from literal group by name having count((name)) > 1 ); So, is it possible to select

What's the difference between select_related and prefetch_related in Django ORM?

℡╲_俬逩灬. 提交于 2019-11-26 11:01:01
In Django doc, select_related() "follows" foreign-key relationships, selecting additional related-object data when it executes its query. prefetch_related() does a separate lookup for each relationship, and does the "joining" in Python. What does it mean by "doing the joining in python"? Can someone illustrate with an example? My understanding is that for foreign key relationship, use select_related ; and for M2M relationship, use prefetch_related . Is this correct? Your understanding is mostly correct. You use select_related when the object that you're going to be selecting is a single object