I've added a MultivaluedField to my index (haystack), I need to search for a ManyToMany related field, but it doesn't work.
The engine is WHOOSH.
This how my index looks like:
class PostIndex(SearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='author')
body = CharField(model_attr='body')
pub_date = DateTimeField(model_attr='publish')
regions = MultiValueField()
def prepare_regions(self, obj):
return [region.name for region in obj.regions.all()]
And this how my model looks like:
class Post(models.Model):
title = models.CharField(_('title'), max_length=200)
author = models.ForeignKey(User, blank=True, null=True)
body = models.TextField(_('body'), )
allow_comments = models.BooleanField(_('allow comments'), default=True)
publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
categories = models.ManyToManyField(Category, blank=True)
tags = TagField()
objects = PublicManager()
regions = models.ManyToManyField(Region, blank=True)
If I use SearchQuerySet().filter(region__in=words_list) it works. The problem is that I don't know when the user is searching for a region or another field, so I have to use SearchQuerySet().filter(content__icontains=words_list). And in this way nothing is found.
Thanks
Thanks!!
Try :
class PostIndex(SearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='author')
body = CharField(model_attr='body')
pub_date = DateTimeField(model_attr='publish')
regions = CharField(model_attr='regions')
You only add region id to the index for Region.
Try
def prepare_regions(self, obj):
return [region.pk for region in obj.regions.all()]
Related
I have two different models. HitCount model stores IP addresses whose was viewed Post. And what i want is filtering popular 3 posts which viewed more. I've tried some queries but i couldn't. I am sharing my models with you.
class Post(ModelMeta, models.Model):
title = models.CharField(max_length=255, verbose_name='Başlık', unique=True)
slug = models.SlugField(max_length=255, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='blog_posts', verbose_name="Yazarı")
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='blog_posts',
verbose_name="Kategorisi", null=True)
tags = models.ManyToManyField(Tag, related_name='blog_posts', verbose_name='Etiketler')
image = models.ImageField(verbose_name='Fotoğraf (800x460)')
content = RichTextField()
description = models.TextField(null=True)
status = models.IntegerField(choices=STATUS, default=0, verbose_name='Yayın Durumu')
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Oluşturulma Tarihi')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Güncellenme Tarihi')
#property
def get_hit_count(self):
return HitCount.objects.filter(post=self).count()
class HitCount(models.Model):
ip_address = models.GenericIPAddressField()
post = models.ForeignKey("Post", on_delete=models.CASCADE)
def __str__(self):
return f'{self.ip_address} => {self.post.title}'
You can try something like this :
most_viewed_posts = Post.objects.all().order_by('-get_hit_count')[3]
I don't think that you can order by 'get_hit_count', but I think those questions can help you : Django order_by a property
Using a Django custom model method property in order_by()
I did what i want with sorted method. Thanks Alexandre Boucard for the resources.
Solution;
sorted(Post.objects.filter(status=1), key=lambda a: a.get_hit_count, reverse=True)
reverse=False as a default and it sorts ascending in this case i want to get reversed so i used reverse=True
i have two models ImageShoot and Image.
models.py:
class ImageShoot(models.Model):
name = models.CharField(max_length=100)
# image = models.URLField(name=None)
created_at = models.TimeField(auto_now_add=True)
def __str__(self):
return self.name
class Image(models.Model):
license_type = (
('Royalty-Free','Royalty-Free'),
('Rights-Managed','Rights-Managed')
)
image_number = models.CharField(default=random_image_number,max_length=12)
title = models.CharField(max_length = 100)
image = models.ImageField(upload_to = 'home/tboss/Desktop/image' , default = 'home/tboss/Desktop/image/logo.png')
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
shoot = models.ForeignKey(ImageShoot, on_delete=models.CASCADE)
image_keyword = models.TextField(max_length=1000)
credit = models.CharField(max_length=150, null=True)
location = models.CharField(max_length=100, null=True)
license_type = models.CharField(max_length=20,choices=license_type, default='')
uploaded_at = models.TimeField(auto_now_add=True)
def __str__(self):
return self.title
admin.py:
class Imageset(admin.ModelAdmin):
associated_images = ImageShoot.image_set.all()
return associated_images
admin.site.register(Image)
admin.site.register(ImageShoot,Imageset)
what i am trying to achieve that when i create a image it should show on imageshoot also like when i create image in shoot 1. this image should show on shoot 1.
i am not sure which field should i add on imageshoot.
Use Reverse lookup
In Your views you can get all the images associated with the ImageShoot Obj using set.Try this in your shell after briefly going through the docs
associated_images = ImageShoot.image_set.all()
You can also use django orm methods for querysets like filter, count, etc.
Edit:
To display related images you can use this in admin:
#admin.register(ImageShoot)
class Imageset(admin.ModelAdmin):
list_display = ('name', 'created_at', 'associated_images')
def associated_images(self, obj):
return obj.image_set.all() #<----edit
associated_images.admin_order_field = 'imageshoot_image'
I want to create a custom object list in the view and pass it to the template. In the template I want to loop over the list and display the information.
My models are
class CustomUser(AbstractUser):
def __str__(self):
return self.email
class Post(models.Model):
author = models.ForeignKey(CustomUser,on_delete=models.CASCADE,)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
post_url = models.URLField(max_length = 200, blank = True)
slug = models.SlugField(unique=True, blank=True)
class subscription(models.Model):
creator = models.ForeignKey(CustomUser,default=None, null=True,on_delete=models.CASCADE,related_name='creator',)
booster = models.ForeignKey(CustomUser,default=None, null=True,on_delete=models.CASCADE,related_name='booster')
sub_value = models.FloatField(blank = True)
sub_id = models.TextField(blank = True)
status = models.BooleanField(default=False)
dateSubscribed = models.DateTimeField(default=timezone.now)
dateSubscriptionEnded = models.DateTimeField(default=timezone.now)
paymentCount = models.FloatField(default= 0)
I want to filter objects from subscription model like below
subs = subscription.objects.filter(booster = request.user)
Then find creators in the above subs object list and for each creator get the name, numbers Posts, and number of Subscribers. Add this to custom list and pass it to the template to loop over and display the information in the template. Can someone help me how to create this custom list. Thanks!
Ok so here are the basics minus the subscribers because I don't see the relation clearly. This is how to parse the name and the number of posts. \
my_list = []
for sub in subs:
name = sub.creator.name
auth_id = sub.creator.id
posts = Post.objects.filter(author=auth_id)
num_of_posts = len(posts)
my_list.append({
'name':name,
'post_count': num_of_posts,
})
then you would pass mylist thru the template context.
It is a common mistake to name the related_name=… parameter [Django-doc] to the same value as the name of the field. The related_name parameter however is the name of the reverse relation Django will automatically add. So here it means a relation to access for example the related subscription objects of a given CustomUser.
Therefore it makes more sense to rename these, for example like:
class Subscription(models.Model):
creator = models.ForeignKey(
CustomUser,
default=None,
null=True,
on_delete=models.CASCADE,
related_name='created_subscriptions'
)
booster = models.ForeignKey(
CustomUser,
default=None,
null=True,
on_delete=models.CASCADE,
related_name='boosted_subscriptions'
)
sub_value = models.FloatField(blank=True)
sub_id = models.TextField(blank =True)
status = models.BooleanField(default=False)
dateSubscribed = models.DateTimeField(default=timezone.now)
dateSubscriptionEnded = models.DateTimeField(default=timezone.now)
paymentCount = models.FloatField(default=0)
Next we can make a query where:
from django.db.models import Count
CustomUser.objects.filter(
created_subscriptions__booster=request.user
).annotate(
number_of_posts=Count('post', distinct=True)
)
This is a QuerySet of CustomUsers where each CustomUser that arises from this QuerySet has an extra attribute .number_of_posts that contains the number of posts. You thus can iterate over the queryset directly in the template.
I'm new to Django and I'm trying to get the item from the database.
The problem is the item is saved as a list in Django. And item[i] is not helping either. Please help me figure it out how to get the data
in the shell, I have tried
for x in NewsItem.objects.all():
print(x.tag)
this will print
['babi kutil', 'babi hutan', 'babi liar', 'hutan indonesia']
['ibu aniaya anak']
['pilkada serentak 2018', 'bandung', 'nurul arifin', 'pilwalkot bandung 2018']
['narkoba di surabaya', 'bnnp jatim']
['pilkada serentak 2018', 'banten']
But I want to get each item, not as a list.
The NewsItem Model.
class NewsItem(models.Model):
breadcrumbs = models.CharField(max_length=150, null=True)
penulis = models.CharField(max_length=100, null=True)
judul = models.CharField(max_length=200, null=True)
berita = models.TextField()
tag = models.TextField(null=True)
url = models.CharField(max_length=200, unique = True)
website = models.CharField(max_length=50, null=True)
date = models.DateTimeField()
#property
def to_dict(self):
data = {
'data': json.loads(self.url),
'tanggal': self.tanggal
}
return data
def __str__(self):
return self.url
You can use 2 for loops to achieve this and as you have mentioned tag as TextField, so you need to split it on comma.
for x in NewsItem.objects.all():
tag_text = x.tag[1:-1]
tag_list = tag_text.split(",")
for tag in tag_list:
print(tag)
curr = curr = get_object_or_None('Page', menu__slug=slug1, menu__parent__slug=slug2).prefetch_related('menu')
Need to use prefetch_related to get info from ForeignKey like an object and get None if it is not in table. Who knows, can I realize it by using get_object_or_None? It seems to work, but read, that it will not. Why so?
class Menu(models.Model):
name = models.CharField()
parent = models.ForeignKey('self', related_name='children')
slug = models.SlugField()
region = models.ForeignKey()
pos = models.IntegerField()
on_top = models.BooleanField()
nofollow = models.BooleanField()
class Page(models.Model):
title = models.CharField()
name = models.CharField()
menu = models.ForeignKey('Menu')
meta_key = models.TextField()
meta_desc = models.TextField()
body = models.TextField()
has_certificate = models.BooleanField()
get_object_or_None() is meant to be a shortcut to specifying a try except block, and it doesn't return a QuerySet, the class where prefetch_related() is from.
Also, if you're trying to get just one instance of your model you're not getting any performance enhancement by prefetching, in fact you'll make your query heavier on the db.