Skip to content

Instantly share code, notes, and snippets.

@Olegblow
Created March 26, 2020 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olegblow/9d2da2a26ac7136358c7717315a498af to your computer and use it in GitHub Desktop.
Save Olegblow/9d2da2a26ac7136358c7717315a498af to your computer and use it in GitHub Desktop.
view.py
Form:
class BaseFilter(django_filters.FilterSet):
"""Базовый класс фильтров ."""
def __init__(self, *args, **kwargs):
prefix_slug = kwargs.pop('prefix_slug', None)
super().__init__(*args, **kwargs)
if prefix_slug is not None:
self.set_filter_choices(prefix_slug)
else:
...
#
any_lvl0 = django_filters.ChoiceFilter(
widget=CustomSelect(
attrs={'placeholder': 'any1', 'with_links': True}
),
choices=[]
)
any_lvl2 = django_filters.ChoiceFilter(
widget=CustomSelect(
attrs={'placeholder': 'any2', 'with_links': True}
),
choices=[]
)
city = django_filters.CharFilter(
widget=FormCityInput(attrs={'placeholder': 'Город'}),
lookup_expr='icontains',
)
...
def any_choices(self, qs):
"""Создаем чойзы для виджета"""
return (
(any.get_slug_url(self.career_section), any.categories)
for any in qs
)
Serializer:
class ArtistSerializer(serializers.ModelSerializer):
"""Сериализатор модели Artist."""
movie_set = MoveSetSerializer(many=True)
social_networks = SocialNetworkSerializer(many=True)
class Meta:
model = Artist
fields = ('id', 'full_name', 'nickname', 'gender',
'birthday', 'about', 'movie_set', 'social_networks')
def create(self, validated_data):
movies = validated_data.pop('movie_set')
artist = Artist.objects.create(**validated_data)
for movie in movies:
movie_create = Movie.objects.create(**movie)
movie_create.artists.add(artist)
return artist
View:
class BaseListView(ListView):
"""Базовый класс списка."""
paginate_by = 30
ordering = '-any'
@abstractmethod
def _get_any_counter(self) -> Count:
"""Фильтр по."""
def get_context_data(self, *args, object_list=None, **kwargs) -> Dict:
context = super().get_context_data(
*args, object_list=object_list, **kwargs
)
any = Area.objects.annotate(
counter=self._get_any_counter()
).values('id', 'name', 'counter').order_by('-counter')
)
context['any'] = any
context['any2'] = any
return
return context
class AnyEdit(AnyBase, UpdateView):
"""Редактирование."""
permission_required = 'app.edit_any'
def __init__(self):
super().__init__()
self.success_url += '?edit=True'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
current_user = self.request.user:
if current_user.is_authenticated:
context['any_extra'] = extra()
...
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment