Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass data (specific fiiled) from views.py to models.py

I have a problem. How can i pass the data (def maybe) from models.py
I need this for filter by category in future

class Tag(models.Model):
.......
    category = models.ForeignKey(Category, null=True, on_delete=models.PROTECT, related_name='category', verbose_name='Tag category')
......

def get_category(self):
return self.category

To views.py. This is it doesn’t work

class GetDetailTag(DetailView):
model = Tag
template_name = 'main/catalog.html'
context_object_name = 'tag'
category = Tag.get_category



def get_context_data(self, *, object_list=None, **kwargs):
    context = super().get_context_data(**kwargs,)
    context['pansion_in_tag_list'] = Pansions.objects.filter(tags__slug=self.kwargs['slug'])
    context['tags_in_category'] = Tag.objects.filter(category__slug = '...INSERT THE DATA FROM MODEL HERE...')
    return context

I was trying to call the ‘def'(get_category) in views.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Anyway? How i can to do that?

>Solution :

You can just obtain this from the tag, so:

{{ tag.category }}

or in the DetailView:

class TagDetailView(DetailView):
    model = Tag
    template_name = 'main/catalog.html'
    context_object_name = 'tag'

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(object_list=object_list, **kwargs)
        context['pansion_in_tag_list'] = Pansions.objects.filter(
            tags__slug=self.kwargs['slug']
        )
        context['tags_in_category'] = Tag.objects.filter(
            category_id=self.object.category_id
        )
        # category = self.object.category (obtain the category)
        return context
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading