My model is like this
class NounPlural(models.Model):
idnoun_plural = models.IntegerField(db_column='idNoun_Plural', primary_key=True) # Field name made lowercase.
nns = models.TextField(db_column='NNS', blank=True) # Field name made lowercase.
news_idnews = models.ForeignKey(News, db_column='news_idnews')
class News(models.Model):
idnews = models.IntegerField(primary_key=True)
source = models.TextField(blank=True)
title = models.TextField(blank=True)
My view.py is like this
def allobj(request):
obj_json = serializers.serialize('json', NounPlural.objects.select_related('news_idnews')[:5] )
obj_list = json.loads( obj_json )
json_data = json.dumps( obj_list )
return HttpResponse( json_data, content_type='application/json' )
All I want to do is to get the NounPlural and also the title from News.When I go to allobj link I only get NounPlural.
In the django documentation related to your question, I always see the need for a .get(id=<something>) with the select_related() call, like so:
Book.objects.select_related('person__city').get(id=4)
So shouldn't your call be:
NounPlural.objects.select_related('news_idnews').get(id=<Newsid>)[:5]
Is this the reason you only have a NounPlural and not the QuerySet you want ?
Related
The goal of this project is to create an API that refreshes hourly with the most up to date betting odds for a list of games that I'll be scraping hourly from the internet. The goal structure for the JSON returned will be each game as the parent object and the nested children will be the top 1 record for each of linesmakers being scraped by updated date. My understanding is that the best way to accomplish this is to modify the to_representation function within the ListSerializer to return the appropriate queryset.
Because I need the game_id of the parent element to grab the children of the appropriate game, I've attempted to pull the game_id out of the data that gets passed. The issue is that this line looks to be populated correctly when I see what it contains through an exception, but when I let the full code run, I get a list index is out of range exception.
For ex.
class OddsMakerListSerializer(serializers.ListSerializer):
def to_representation(self, data):
game = data.all()[0].game_id
#if I put this here it evaluates to 1 which should run the raw sql below correctly
raise Exception(game)
data = OddsMaker.objects.filter(odds_id__in = RawSQL(''' SELECT o.odds_id
FROM gamesbackend_oddsmaker o
INNER JOIN (
SELECT game_id
, oddsmaker
, max(updated_datetime) as last_updated
FROM gamesbackend_oddsmaker
WHERE game_id = %s
GROUP BY game_id
, oddsmaker
) l on o.game_id = l.game_id
and o.oddsmaker = l.oddsmaker
and o.updated_datetime = l.last_updated
''', [game]))
#if I put this here the data appears to be populated correctly and contain the right data
raise Exception(data)
data = [game for game in data]
return data
Now, if I remove these raise Exceptions, I get the list index is out of range. My initial thought was that there's something else that depends on "data" being returned as a list, so I created the list comprehension snippet, but that doesn't resolve the issue.
So, my question is 1) Is there an easier way to accomplish what I'm going for? I'm not using a postgres backend so distinct on isn't available to me. and 2) If not, its not clear to me what instance is that's being passed in or what is expected to be returned. I've consulted the documentation and it looks as though it expects a dictionary and that might be part of the issue, but again the error message references a list. https://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior
I appreciate any help in understanding what is going on here in advance.
Edit:
The rest of the serializers:
class OddsMakerSerializer(serializers.ModelSerializer):
class Meta:
list_serializer_class = OddsMakerListSerializer
model = OddsMaker
fields = ('odds_id','game_id','oddsmaker','home_ml',
'away_ml','home_spread','home_spread_odds',
'away_spread_odds','total','total_over_odds',
'total_under_odds','updated_datetime')
class GameSerializer(serializers.ModelSerializer):
oddsmaker_set = OddsMakerSerializer(many=True, read_only=True)
class Meta:
model = Game
fields = ('game_id','date','sport', 'home_team',
'away_team','home_score', 'away_score',
'home_win','away_win', 'game_completed',
'oddsmaker_set')
models.py:
class Game(models.Model):
game_id = models.AutoField(primary_key=True)
date = models.DateTimeField(null=True)
sport=models.CharField(max_length=256, null=True)
home_team = models.CharField(max_length=256, null=True)
away_team = models.CharField(max_length=256, null=True)
home_score = models.IntegerField(default=0, null=True)
away_score = models.IntegerField(default=0, null=True)
home_win = models.BooleanField(default=0, null=True)
away_win = models.BooleanField(default=0, null=True)
game_completed = models.BooleanField(default=0, null=True)
class OddsMaker(models.Model):
odds_id = models.AutoField(primary_key=True)
game = models.ForeignKey('Game', on_delete = models.CASCADE)
oddsmaker = models.CharField(max_length=256)
home_ml = models.IntegerField(default=999999)
away_ml = models.IntegerField(default=999999)
home_spread = models.FloatField(default=999)
home_spread_odds = models.IntegerField(default=9999)
away_spread_odds = models.IntegerField(default=9999)
total = models.FloatField(default=999)
total_over_odds = models.IntegerField(default=999)
total_under_odds = models.IntegerField(default=999)
updated_datetime = models.DateTimeField(auto_now=True)
views.py:
class GameView(viewsets.ModelViewSet):
queryset = Game.objects.all()
serializer_class = GameSerializer
Thanks
To answer the question in the title:
The instance being passed to the Serializer.to_representation() is the instance you pass when initializing the serializer
queryset = MyModel.objects.all()
Serializer(queryset, many=True)
instance = MyModel.objects.all().first()
Serializer(data)
Usually you don't have to inherit from ListSerializer per se. You can inherit from BaseSerializer and whenever you pass many=True during initialization, it will automatically 'becomeaListSerializer`. You can see this in action here
To answer your problem
from django.db.models import Max
class OddsMakerListSerializer(serializers.ListSerializer):
def to_representation(self, data): # data passed is a queryset of oddsmaker
# Do your filtering here
latest_date = data.aggregate(
latest_date=Max('updated_datetime')
).get('latest_date').date()
latest_records = data.filter(
updated_date_time__year=latest_date.year,
updated_date_time__month=latest_date.month,
updated_date_time__day=latest_date.day
)
return super().to_representation(latest_records)
I'm trying to save Authors model, but it keeps saving twice into the database.
views.py
........................
for book_data in data['items']:
volume_info = book_data['volumeInfo']
title = volume_info['title']
genres = volume_info.get('categories')
authors = volume_info.get('authors')
description = volume_info.get('description')
if not Books.objects.filter(title=title).exists():
book = Books.objects.create(title=title, description=description)
# Does authors exists in database?
existing_authors = Authors.objects.filter(author_name__in=authors)
existing_authors_names = {authors.author_name for authors in existing_authors}
# Create a list of missing authors
missing_authors = [
Authors(author_name=author_name)
for author_name in authors
if author_name not in existing_authors_names
]
# Creating author before adding it to relation
if missing_authors:
missing_authors = Authors.objects.bulk_create(missing_authors)
print(missing_authors)
for m in missing_authors:
m.save()
# Adding to relation
book.authors.add(*existing_authors, *missing_authors)
..........................
I think the problem is in for m in missing_authors right?
models.py
class Authors(models.Model):
author_name = models.CharField(max_length=200)
def __str__(self):
return self.author_name
class Books(models.Model):
title = models.CharField(max_length=300)
description = models.TextField(blank=True, null=True)
authors = models.ManyToManyField(Authors, blank=True)
genres = models.ManyToManyField(Genres, blank=True)
def __str__(self):
return self.title
database is sqllite3
Django version is 2.2.1
The bulk_create method automatically saves the results after the query is executed.
Change your code to this:
if missing_authors:
Authors.objects.bulk_create(missing_authors)
''' remove these lines
for m in missing_authors:
m.save()
#not sure what this line is doing exactly, but it might be causing your problem
book.authors.add(*existing_authors, *missing_authors)
'''
Update
If you can set unique=True for your author_name column, then try the following:
class Authors(models.Model):
author_name = models.CharField(max_length=200, unique=True)
Authors.objects.bulk_create(missing_authors, ignore_conflicts=True)
for m in missing_authors:
m.save()
book.authors.add(*existing_authors, *missing_authors)
I'm very confused about this right now,
so I know when there's a simple code like the below
def text_detail(request ,course_pk, step_pk):
step = get_object_or_404(Text, course_id = course_pk, pk=step_pk)
course_pk and step_pk from the url, and those requests are set equal to course_id and pk here. but what I don't understand is what is course_id and pk here? I mean, course_id is from Course model which is foreignkey to step. so it's self.Course.id so it's course_id. But then, how about the next one pk? shouldn't it be step_id = step_pk? when it's just pk how does django know which pk it is?
Sorry if the question is very confusing, I'm very confused right now.
Edit
class Step(models.Model):
title = models.CharField(max_length=200)
description = models.CharField()
order = models.IntegerField(default=0)
course = models.ForeignKey(Course)
class Meta:
abstract = True
ordering = ['order',]
def __str__(self):
self.title
class Text(Step):
content = models.TextField(blank=True, default="")
Actually the get_or_404() method doing a similar/exact job as below,
try:
return Text.object.get(pk=step_pk,course_id = course_pk)
except Text.DoesNotExist:
raise Http404
You can read the source code of the same here
What is course_id and pk ?
Both are attributes of your Text model, as the name indicates pk is your Primary Key of Text model and course_id is the id/pk of course field which is a FK.
EDIT
Text is inherited from Step model so, it will show properties of usual python class.Hence, the Text model be like this internally (not-exact)
class Text(models.Model):
content = models.TextField(blank=True, default="")
title = models.CharField(max_length=200)
description = models.CharField()
order = models.IntegerField(default=0)
course = models.ForeignKey(Course)
class Meta:
ordering = ['order', ]
def __str__(self):
return self.title
Example
text = Text.objects.get(id=1) # text instance with id=1
text.course_id # will hold the id of "course" instance which is related to the particular "text" instance
URL assignment and all those stuffs are entirely depends on your choice and logic. So If you need to get a Text instance in your view, do as below,
text = get_object_or_404(Text, pk = pk_of_TEXT_instance)
I have such a model, in my django application. I want to draw only one field of this model and put them in the view. My solution below does not work:
obj = Text.objects.get(subsID)
My model
result = braintree.Subscription.create({
"payment_method_token": payment_method_token,
"plan_id": "67mm"
})
subscription_id = result.subscription.id
class Text(models.Model):
title = models.CharField(max_length=255)
text = models.TextField()
date_from = models.DateTimeField('date from', blank=True, null=True)
date_to = models.DateTimeField('date to', blank=True, null=True)
subsID = models.CharField(default=subscription_id, max_length=255)
def __unicode__(self):
return self.title
My view
def get_history(request):
subscription_id = Text.objects.filter(subsID)
history = braintree.Subscription.find(subscription_id)
return render(request, "sheet/history.html", {"history": history})
Generally, When filter or get, you have to put query inside it, like
subscription_id = Text.objects.filter(fieldname="searchterm")
This will return a queryset.So to view this
subscription_id.values() #returns a list of objects(dicts)
If you want to get only subsID
subscription_id.values("subsID")
This also return you list which contains
[{"subsID":"value"}, {"subsID":"value"} ....]
If you want to get only values
subscription_id.values_list("subsID", flat=True)
This will return like
["value", "value", ....]
You have to equal subsID to the value you want to find.
subscription_id = Text.objects.filter(subsID=<your subscrition id variable>)
pay attention this will return a list []
subscription_id = Text.objects.get(subsID=<your subscrition id variable>)
This will return an object
Link
You can't use the model in the view, you need to use the ModelForm or Form.
Once you use that one, you can specify which field is active or not or simply setting the attribute in the ModelForm,
exclude=['paramiter_name']
and it's done.
Good luck.
How to create an object for a Django model with a many to many field?
From above question i come to know we can save Many to Many field later only.
models.py
class Store(models.Model):
name = models.CharField(max_length=100)
class Foo(models.Model):
file = models.FileField(upload_to='')
store = models.ManyToManyField(Store, null=True, blank=True)
views.py
new_track.file = request.FILES['file']
new_track.save()
And file uploading working fine then later i modify my code to add store then i am here...
Now i am sure db return id's here. Then i tried with my below code but that's given me error only
x = new_track.id
new = Foo.objects.filter(id=x)
new.store.id = request.POST['store']
new.save()
ok so the error here is 'QuerySet' object has no attribute 'store'
And also i tried with add that's now working either.
So the question is how to save()
the right way of saving objects with manytomany relations would be:
...
new_track.file = request.FILES['file']
new_track.save()
new_store = Store.objects.get(id=int(request.POST['store']))
new_track.store.add(new_store)
As of 2020, here's my approach to saving ManyToMany Field to a given object.
Short Answer
class HostingRequestView(View):
def post(self, request, *args, **kwargs):
form = VideoGameForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.updated_by = request.user
obj.save()
selected_categories = form.cleaned_data.get('category') #returns list of all selected categories e.g. ['Sports','Adventure']
#Now saving the ManyToManyField, can only work after saving the form
for title in selected_categories:
category_obj = Category.objects.get(title=title) #get object by title i.e I declared unique for title under Category model
obj.category.add(category_obj) #now add each category object to the saved form object
return redirect('confirmation', id=obj.pk)
Full Answer
models.py
class Category(models.Model):
title = models.CharField(max_length=100, null=True, unique=True)
class VideoGame(models.Model):
game_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, blank=False, null=False)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, on_delete=models.CASCADE)
category = models.ManyToManyField(Category) #ManyToMany Category field
date_added = models.DateTimeField(auto_now_add=True, verbose_name="date added")
forms.py ModelForm
class VideoGameForm(forms.ModelForm):
CATEGORIES = (
('Detective', 'Detective'),
('Sports', 'Sports'),
('Action', 'Action'),
('Adventure', 'Adventure'),
)
category = forms.MultipleChoiceField(choices=CATEGORIES, widget=forms.SelectMultiple())
class Meta:
model = VideoGame
fields = ['name', 'category', 'date_added']
views.py on POST
class HostingRequestView(View):
def post(self, request, *args, **kwargs):
form = VideoGameForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.updated_by = request.user
obj.save()
selected_categories = form.cleaned_data.get('category') #returns list of all selected categories e.g. ['Sports','Adventure']
#Now saving the ManyToManyField, can only work after saving the form
for title in selected_categories:
category_obj = Category.objects.get(title=title) #get object by title i.e I declared unique for title under Category model
obj.category.add(category_obj) #now add each category object to the saved form object
return redirect('confirmation', id=obj.pk)
URL path for redirect
urlpatterns = [
path('confirmation/<int:id>/', Confirmation.as_view(), name='confirmation'),
]
I hope this can be helpful. Regards
new.stores.all()
returns all stores linked to the object.
Maybe:
Change Foo to Tracks
Tracks.objects.filter(id=x) to Tracks.objects.get(id=x)
Let me know how it goes
why this confusion so much.. you are getting the id there then, call the store like
new_track.save()
new_track.store.add(request.POST['store'])