These are chat archives for django/django

8th
Jan 2016
Olexandr Shalakhin
@shalakhin
Jan 08 2016 15:01
I have such code in forms.py:
    colors = forms.MultipleChoiceField(
        choices=[
            (color.id, color.name) for color in Color.objects.all()
        ],
        widget=forms.CheckboxSelectMultiple, required=False)
    categories = forms.MultipleChoiceField(
        choices=[
            (category.id, category.name) for category in Category.objects.all()
        ],
        widget=forms.CheckboxSelectMultiple, required=False)
If I try to run migration I get errors
that there is no such database table as Color or Category
How should I write such things to not have to comment something in forms.py?
Jussi Räsänen
@jrasanen
Jan 08 2016 15:03
Do you have a model for color / category?
Olexandr Shalakhin
@shalakhin
Jan 08 2016 15:03
Yes
and I have migrations
Jussi Räsänen
@jrasanen
Jan 08 2016 15:03
can you do Color.objects.all() in django shell?
or do you get an error
Olexandr Shalakhin
@shalakhin
Jan 08 2016 15:03
I have model in code but no tables. Database is clean
to create tables I need to run migrations
but manage.py migrate gives me an error that there is no such table
Jussi Räsänen
@jrasanen
Jan 08 2016 15:04
Run make migrations?
What do you have in your models? What’s the error?
Olexandr Shalakhin
@shalakhin
Jan 08 2016 15:06
Imagine you write you project. You write model and generate migration. Then you run migration and it fails to run because in forms.py you used model for choices
Dan Gamble
@dan-gamble
Jan 08 2016 15:08
Don't you have to move that code into the __init__ method?
Olexandr Shalakhin
@shalakhin
Jan 08 2016 15:08
Maybe I am not alone in such problem so I wonder what is the best practice here? Maybe I did it the wrong way.. I guess comprehension executes query and it failes
I put forms logic in forms.py file
Dan Gamble
@dan-gamble
Jan 08 2016 15:08
I've had the problem before and i fixed it by moving it to the form __init__
Olexandr Shalakhin
@shalakhin
Jan 08 2016 15:08
hm, I will try. Thank you
Puneet Aggarwal
@puneetagg
Jan 08 2016 17:48
Hi...
Please help me in finding error in the following as Django is unable to recognize get_queryset() method:
class UserPostList(generics.ListAPIView):
    model = Post
    serializer_class = PostSerializer
    logging.debug("hello")

    def get_queryset(self):
        queryset = super(UserPostList, self).get_queryset()
        return queryset.filter(author__username=self.kwargs.get('username'))
Nasaruddin
@pace-noge
Jan 08 2016 19:43
This message was deleted
Are you using DRF?
agus makmun
@agusmakmun
Jan 08 2016 20:19

somebody can help me how to add class active for html templates, eg: nav menus, bassed in generic.ListView?

some idea i was try with {% if request.resolver_match.url_name == "name" %} and {% if request.get_full_path == '/mypage/' %} but it isn't work.. i think may it because pagination in generic.ListView...

any idea how to solved it?

Puneet Aggarwal
@puneetagg
Jan 08 2016 20:30
@pace-noge Yes.
Nasaruddin
@pace-noge
Jan 08 2016 22:04

@agusmakmun how about using templatetags?, for example:
assuming your url like this:

url(r'^direktorat/$', DirektoratListView.as_view(), name="direktorat-list"),

templatetags

from django import template

register = template.Library()

@register.simple_tag
def active(request, pattern):
    import re
    if re.search(pattern, request.path):
        return "active"
    return ""

then you can use it at template like this:

{% load active %} # load the templatetags
{% url 'direktorat-list' as direktorat_list_url %}  #save it as variable for future change

<!-- link -->
<li class="{% active request direktorat_list_url %}"><a href="{{ direktorat_list_url }}">Direktorat</a></li>
Nasaruddin
@pace-noge
Jan 08 2016 22:12
@puneetagg how about:
    def get_queryset(self):
        return Post.objects.filter(author__username=self.kwargs.get('username'))