django-queryset

how to specify values in objects in django: is [] not equal to None?

早过忘川 提交于 2019-12-11 19:52:02
问题 how can i specify or determine the values in my objects in python if it has this value [] or it has [<Booking: arab>] , let us say that in my queryset, i filter a value that not exist in my objects. here is what i have done : this date_select has no data >>> x = Booking.objects.filter(date_select='2011-12-3') >>> print x [] >>> if x == None: ... print 'none' ... >>> is it [] not equal to None? when i try a date_select that has data >>> x = Booking.objects.filter(date_select='2011-12-2') >>>

Creating serializer to display correct data

坚强是说给别人听的谎言 提交于 2019-12-11 19:14:44
问题 I have 4 models. User , Question , Choice , and Voting . This is basically a polls app I'm trying to create. A question can have many choices. The Voting model tracks what each user chose as their answer. What I'd like to do is retrieve all the questions and also check what choice the logged in user selected for each question. Here's the models: class Question(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) status = models.CharField(max_length=200) total_votes = models

Cascading Delete w/ Custom Model Delete Method

孤者浪人 提交于 2019-12-11 18:34:35
问题 I have a custom delete method on my Model that I make sure is called correctly when calling delete on the QuerySet by using: Custom delete method on queryset. This does not seem to work when Django performs a cascading delete. In that case, the ORM calls _raw_delete on a regular QuerySet thereby bypassing my custom delete method. How do I prevent that from happening? The issue seems to be caused because this uses _base_manager rather than _default_manager : def related_objects(self, related,

How can I filter a queryset based upon two manytomany relationships having one object in common?

爱⌒轻易说出口 提交于 2019-12-11 18:34:27
问题 Suppose I have a queryset of animals and I want to write a query that determines if a manytomany field on another model has at least one object in common with the animals queryset. How can this be acheived? farm_animals = Animals.objects.filter(name__in=["Dog", "Cow", "Horse"]) print farm_animals # [<Animal: Dog>, <Animal: Cow>, <Animal: Horse>] # Returns all people who have at least one farm animal. people_with_a_farm_animal = People.objects.filter(???) This seems like it should be easy to

Django Queryset in forms

本秂侑毒 提交于 2019-12-11 18:10:11
问题 How can i make a queryset in this modelform. This is my code. Class Sample(forms.ModelForm): class Meta: model = Customer fields = ('name','address',) widgets = { 'name' : Select(attrs={'class':'span2'}), 'address' : TextInput(attrs={'class':'span4'}), } queryset = {'name': User.objects.filter(type_id=1)} Is this the right way in using queryset? Pls help me. Thank You. 回答1: Class Sample(forms.ModelForm): class Meta: model = Customer fields = ('name','address',) widgets = { 'name' : Select

ADDing Django's Q() objects and get two exclusive JOIN ONs

守給你的承諾、 提交于 2019-12-11 17:33:37
问题 So this is the scenario: class Person(models.Model): ... class Aktion(models.Model): ... class Aktionsteilnahme(models.Model): person = models.ForeignKey(Person) aktion = models.ForeignKey(Aktion) The problem now is, that I'm dynamically constructing rather complex queries based on Q() -objects. They end up like this: Person.objects.filter( Q((Q()|Q(aktionsteilnahme__aktion=302))& (Q()|Q(aktionsteilnahme__aktion=547))) ) which can (and automatically will) be reduced to: Person.objects.filter(

How can get obj between two date range for each date

为君一笑 提交于 2019-12-11 16:58:44
问题 I have data in Postgres DataBase like this based on my Model Event | id | name | start_date | end_date | 1 Event1 2018-09-14 14:22:00 2018-09-15 14:22:00 2 Event2 2018-09-15 14:22:00 2018-09-15 15:22:00 I need to return response group_by date and If Event duration (end_date, start_date) took 2 days so i need return him twice in two days and this all should be order by date. So response should look like this: { "2018-09-14": [ { "id": 1, "name": "Event1", "start_date": "2018-09-14 14:22:00",

How to overwrite get method in generic RetrieveAPIView in django rest framework to filter the results

蹲街弑〆低调 提交于 2019-12-11 16:57:07
问题 I have an API that can list several buildings. Each building belongs to several building groups and each building group contains several buildings. I want to show single fields of one building group. More specifically I want to show all buildings of one building group in my RetrieveAPIView. I can list a single BuildingGroup instance using the generic view like so: class BuildingGroupRetrieveAPIView(RetrieveAPIView): serializer_class = BuildingGroupSerializer queryset = BuildingGroup.buildings

Trying to create a tuple of objects create an object and a queryset

♀尐吖头ヾ 提交于 2019-12-11 16:05:13
问题 I need to iterate through a tuple in the template, but from the code i've built i am getting a tuple of an object (album) and a queryset (photo). The problem is how do i iterate over them now in the template? models: class Album(models.Model): title = models.CharField('כותרת', max_length=100, db_index=True) created = models.DateTimeField('תאריך פרסום', auto_now_add=True) creator = models.ForeignKey(User, related_name='galleries_creator', verbose_name='נכתב ע"י') class Photo(models.Model):

combine django model with csv file and loop

做~自己de王妃 提交于 2019-12-11 15:49:02
问题 I finally manage to show csv file within html and also bind with django model but I am missing a for loop but couldn't make it work. if request.method == 'POST' and request.FILES['csv_file2']: myfile = request.FILES['csv_file2'] fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) data = csv.reader(fs.open(filename, mode='r')) lines=[] instances = [] for row in data: line = row[0] lines.append(line) query = line instances.append(FP.objects.filter(FP_Item=query)) pair = zip(lines,