class tags(models.Model):
""" This is the tag model """
tag = models.CharField(max_length=15) # Tag name
tagDescription = models.TextField() # Tag Description
tagSlug = models.CharField(max_length=400) # Extra info can be added to the existing tag using this field
createdAt = models.DateTimeField(auto_now_add=True) # Time at which tag is created
updatedAt = models.DateTimeField(auto_now=True) # Time at which tag is updated
class stores(models.Model):
""" This is the store model """
storeName = models.CharField(max_length=15) # Store Name
storeDescription = models.TextField() # Store Description
storeURL = models.URLField() # Store URL
storePopularityNumber = models.IntegerField(choices=PRIORITY_CHOICES,default=2) # Store Popularity Number
storeImage = models.ImageField(upload_to="images") # Store Image
storeSlug = models.CharField(max_length=400) # This is the text you see in the URL
createdAt = models.DateTimeField(auto_now_add=True) # Time at which store is created
updatedAt = models.DateTimeField(auto_now=True) # Time at which store is updated
storeTags = models.ManyToManyField(tags) # All the tags associated with the store
特定のタグに関連するすべてのストアを検索したい。
for t in tags.objects.all():
print ([str(a.storeName) for a in t.stores_set.all()])
上記のforループを使用することで、すべての店舗に関連するすべてのタグを取得できます...しかし、実際にはDjango rest-frameWorkを使用しています...今、シリアライザーを使用して関連する店舗の名前を返すビューがあります店に...
class tagList(generics.ListAPIView,APIView):
serializer_class = getAllTagsDetailSerializer
def get_queryset(self):
key = self.request.QUERY_PARAMS.get('appKey', None)
getTagName = self.request.QUERY_PARAMS.get('tagName', None)
keyData = app.objects.filter(appKey=key).exists()
try:
if keyData == True:
return tags.objects.filter(tag=getTagName)
else:
raise exceptions.PermissionDenied
except app.DoesNotExist:
pass
`class getAllStoresDetailSerializer(serializers.ModelSerializer):
storeImage = serializers.Field(source='imageURL')
storeTags =serializers.Field(source='StoreTags')
class Meta:
model = stores
fields = ('storeName','storeDescription','storeURL',
'storePopularityNumber','storeImage','storeTags',
'storeSlug','createdAt','updatedAt'
)`
class getAllTagsDetailSerializer(serializers.ModelSerializer):
tagStores = RelatedField(Many=True)
class Meta:
model = tags
fields = ('tagStores'
)
しかし、これは機能していません...誰か助けてください...