class A:
b = ForeignKey(B)
b.a_set.all() # Или filter, get. В общем, обычный QuerySet
{% for contact in node.contact_set.all %}
{{ contact.full_name }}
{% endfor %}
subdivisions = Subdivision.objects.all()
subdivisions = Subdivision.objects.all().prefetch_related('contact_set')
response: {
text: 'Fuel – Sunburn',
audio: {
id: 347465425,
owner_id: 244563967,
artist: 'Fuel',
title: 'Sunburn',
duration: 263,
date: 1424262682,
url: 'https://psv4.vk.m...VSPxjVmkPf16pYqXRAE',
lyrics_id: 14681808,
genre_id: 1
}
}
# Вместо такой конструкции
Item(categories=categories, img=content, name=line[2], text=line[3], options=line[4]).save()
# Лучше использовать
Item.objects.create(categories=categories, img=content, name=line[2], text=line[3], options=line[4])
except Categories.DoesNotExist:
new_user = User.objects.create_user(username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email=self.cleaned_data['email'],
first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
)
new_user.is_active = True
new_user.save()
new_user = User.objects.create_user(username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email=self.cleaned_data['email'],
first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
is_active=True,
)
def modify(value, func):
return func(value)
x1 = modify(x1, lambda x: x**2) # Не сложно догадаться, что это возведение в квадрат.
from django.db.models import Count
Car.objects.annotate(reviews_count=Count('car_rewiews')).filter(reviews_count__gt=N)