django-orm

Django Queryset: Cast VARCHAR field in SQL before filtering

China☆狼群 提交于 2019-12-02 05:01:59
I have a table that I cannot control, but need to select from it. The field "building" is a varchar, and also defined like this in my (non managed) django model. But it should be treated as integer, when selecting from that table (there are values like "000100", and even spaces at the end). I need a simple filter like this: .annotate(CastInt("building")).filter(building__castint__exact=my_id) only problem is, CastInt does not exist. How could one achieve this? I was looking at .extra() (that we should no use anymore, as it's deprecated) and RawSQL, but hoping for a solution using only the

Bulk Create objects with Many to Many Relationship in Django

…衆ロ難τιáo~ 提交于 2019-12-02 02:27:37
问题 I have Contacts and ContactsGroup Two models contact contains group as m2m relationship class ContactGroup(models.Model): contact_group = models.ManyToManyField(ContactGroup, null=True, related_name="contact_list") and contacts is another model I want to create bulk_create contacts where contact_group also added on the model. group_list = [] if group: groups = [item.strip() for item in group.split(',')] for item in groups: try: group, created = ContactGroup.objects.get_or_create(account

What is simplest way join __contains and __in?

廉价感情. 提交于 2019-12-02 01:15:02
I am doing tag search function, user could observe a lot of tags, I get it all in one tuple, and now I would like to find all text which include at least one tag from the list. Symbolic: text__contains__in=('asd','dsa') My only idea is do loop e.g.: q = text.objects.all() for t in tag_tuple: q.filter(data__contains=t) For example: input tuple of tags, ('car', 'cat', 'cinema') output all messages what contains at least one word from that tuple, so My cat is in the car , cat is not allowed in the cinema , i will drive my car to the cinema Thanks for help! Here you go: filter = Q() for t in tag

GeoDjango & SpatiaLite - filtering nearby objects

谁都会走 提交于 2019-12-01 23:35:00
问题 My models.py has User and Business models that have this field: location = PointField(geography=True) I'm getting Google Maps coordinates (in EPSG 4326 ) via Geocode service from an address which the user specifies. Then I'm saving it in the above field (also EPSG 4326 ). Now, what I want is to get all Business objects within a specified radius (of 1km for example), based on the user location: Business.gis.filter(location__distance_lt=(request.user.location, D(km=1))) But it doesn't work, it

GeoDjango & SpatiaLite - filtering nearby objects

旧巷老猫 提交于 2019-12-01 21:37:58
My models.py has User and Business models that have this field: location = PointField(geography=True) I'm getting Google Maps coordinates (in EPSG 4326 ) via Geocode service from an address which the user specifies. Then I'm saving it in the above field (also EPSG 4326 ). Now, what I want is to get all Business objects within a specified radius (of 1km for example), based on the user location: Business.gis.filter(location__distance_lt=(request.user.location, D(km=1))) But it doesn't work, it gives me this error: ValueError: SpatiaLite does not support distance queries on geometry fields with a

Can I remove ORDER BY from a Django ORM query?

烂漫一生 提交于 2019-12-01 15:04:24
It seems like Django by default adds ORDER BY to queries. Can I clear it? from slowstagram.models import InstagramMedia print InstagramMedia.objects.filter().query SELECT `slowstagram_instagrammedia`.`id`, `slowstagram_instagrammedia`.`user_id`, `slowstagram_instagrammedia`.`image_url`, `slowstagram_instagrammedia`.`video_url`, `slowstagram_instagrammedia`.`created_time`, `slowstagram_instagrammedia`.`caption`, `slowstagram_instagrammedia`.`filter`, `slowstagram_instagrammedia`.`link`, `slowstagram_instagrammedia`.`attribution_id`, `slowstagram_instagrammedia`.`likes_count`, `slowstagram

Django custom user field clashes with AbstractBaseUser

血红的双手。 提交于 2019-12-01 13:26:34
I am building a Django project from an existing database. The database is being used by other systems, so I cannot change its schema. This is my current custom User model: class Users(AbstractBaseUser): id_user = models.IntegerField(primary_key=True) role = models.IntegerField() username = models.CharField(max_length=50, unique=True) last_login_date = models.DateTimeField() AbstractBaseUser needs a column named last_login , while current database table has last_login_date column which serves like AbstractBaseUser.last_login . Now I need to use that column in Users.last_login : ... last_login =

Get grandchildren of an object with django

£可爱£侵袭症+ 提交于 2019-12-01 11:01:01
I'm building a webapp with django with the following models: class Business(models.Model): ... class Branch(models.Model): business = models.ForeignKey(Business) ... class Event(models.Model): branch = models.ForeignKey(Branch) My questions is how can I get all events by their business (not their branch), and if it possible to do so in a DB query. Thanks! Bitonator Django querysets allow you to use the "__" notation to access relationships. You can take it to any depth and read more about it here . Django offers a powerful and intuitive way to “follow” relationships in lookups, taking care of

Django Tastypie, ManyToMany Saving Error

大憨熊 提交于 2019-12-01 10:15:51
问题 i got a problem when i'm saving an item, via tastypie api. (POST method) Here is my api.py code. from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS from tastypie.authorization import DjangoAuthorization from tastypie.authentication import BasicAuthentication from tastypie import fields from apps.clients.models import Client from django.contrib.auth.models import User class ClientAPI(ModelResource): users = fields.ToManyField('apps.clients.api.ClientUserAPI', 'users',related

Get grandchildren of an object with django

一世执手 提交于 2019-12-01 09:22:37
问题 I'm building a webapp with django with the following models: class Business(models.Model): ... class Branch(models.Model): business = models.ForeignKey(Business) ... class Event(models.Model): branch = models.ForeignKey(Branch) My questions is how can I get all events by their business (not their branch), and if it possible to do so in a DB query. Thanks! 回答1: Django querysets allow you to use the "__" notation to access relationships. You can take it to any depth and read more about it here.