我有两种模式:
class City(models.Model):
name = models.CharField(max_length=50, verbose_name='Qyteti')
slug = models.SlugField(unique=True)
class Business(models.Model):
name = models.CharField(max_length=120, verbose_name='emri')
slug = models.SlugField(unique=True)
city = models.OneToOneField(City, verbose_name='qyteti')
created = models.DateTimeField(auto_now_add=True, verbose_name='krijuar')
categories = models.ForeignKey(Category, related_name='businesses', verbose_name='kategoria')
user = models.ForeignKey(User, related_name='user_businesses', verbose_name='autori')
geom = gis_models.PointField(u"longitude/latitude", geography=True, blank=True, null=True)
我想创建一个像yelp.com这样的搜索
我希望人们以三种不同的方式进行搜索。
我尝试过来自itertools的链,但到目前为止还没有结果。
我现在只想做一个简单的搜索,而不是外部搜索引擎。
感谢任何人的帮助。
答案 0 :(得分:1)
您需要执行以下操作:
1)模型的变化:
class Business(models.Model):
city = models.ForeignKey(City, verbose_name='qyteti', related_name='businesses')
2)查询:
1) one_type = Business.objects.filter(name = "some_business_type").select_related('city')
one_type.city.name
2) one_city_all_business = City.objects.filter(name = "London").prefetch_related('businesses')
one_city_all_business.businesses.all() - here u get a list of all businesses for London
3) one_type_one_city = City.objects.filter(name = "London").filter(businesses__name = "some_business_type").prefetch_related('businesses')
这里唯一的问题 - 你需要决定用户如何选择要运行的查询,我的意思是你将如何定义用户想要搜索的内容。