0

Here are the models I created: So I have labelled each data item (search result) with risk and now I would like to browse data item by risk within a project. I don't have concrete idea how generic relation works. Looking up on forums I saw people using generic to have tags to results. I am using Django 1.7. I have everything working as of now, but I am not sure how to write the viewset to have search the search result by risk and so that I can query the API from front-end. A search result can have multiple risk tagged to it

enter image description here

`

Risks/models.py

    class Risk(models.Model):
        """Risk for data. Every risk has unique name.
        """
    name = models.CharField(max_length=100, unique=True)
    search_string = models.TextField(blank=True)  # search keywords related to this risk    
    def __unicode__(self):
        return 'Risk[id: {id}, name: {name}]'.format(
            id=self.id, name=self.name)

class RiskItem(models.Model):
    #RiskItem(content_type=article, risk=r)
    risk = models.ForeignKey(Risk)
    project = models.ForeignKey('engagement.Project')
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    def __unicode__(self):
        return "%s :" % (self.risk.name)

I have referred to risk in the search result model by GenericRelation, by taking reference from forums. Here is how it looks like. Here is how my search result is like and each search item is associated with a risk. enter image description here

Search/models.py

from engagement.models import Project
from risk.models import RiskItem
from django.contrib.auth.models import User

class Search(models.Model):

    project = models.ForeignKey(Project, related_name="searches")
    user = models.ForeignKey(User)
    string = models.CharField(max_length=1024) # search string

    created_at = models.DateTimeField(auto_now_add=True)
    def __unicode__(self):
      return self.string


class SearchResult(models.Model):
    search = models.ForeignKey(Search, related_name="results")
    title = models.CharField(max_length=255)
    url = models.URLField(blank=False, max_length=300)
    snippet = models.TextField(blank=True)
    risks = GenericRelation(RiskItem)
    label = models.CharField(max_length=100, default=0)

    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('rank',)

    def __unicode__(self):
       return self.title

`

Want to create a view which list all search results by Risk, by connecting the two apis, single risk can be associated with multiple search result items.

nightrider
  • 95
  • 8
  • Once you have a viewset, you can add [filters to it](http://www.django-rest-framework.org/api-guide/filtering/). – Ross Rogers Nov 25 '15 at 16:51
  • @Ross-Rogers - If I have one api with the search result ids only linked to every risk and every search item associated with multiple risk, then how to create an api, giving me the list of search item all variables given a risk as query ? – nightrider Nov 25 '15 at 19:58

0 Answers0