from django.contrib.postgres.fields import ArrayField
from django.db import models
class ArrayFieldTest(models.Model):
days = ArrayField(base_field=models.DateField(), default=list)
>>> from test.models import ArrayFieldTest
>>> from django.utils import timezone
>>> from dateutil.relativedelta import relativedelta
>>> date_list = [timezone.now().date(), timezone.now().date() + relativedelta(days=1)]
>>> date_list
[datetime.date(2016, 1, 23), datetime.date(2016, 1, 24)]
>>> ArrayFieldTest(days=date_list).save()
>>> ArrayFieldTest.objects.all().first().days
[datetime.date(2016, 1, 23), datetime.date(2016, 1, 24)]
>>> ArrayFieldTest().save()
>>> ArrayFieldTest.objects.all().last().days
[]
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True)
organization = models.ForeignKey(Organization, verbose_name=_('Organization'))
...
AUTH_PROFILE_MODULE = 'app.UserProfile'
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
organization = models.ForeignKey(Organization, verbose_name=_('Organization'))
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(pk=1)
>>> dir(user)
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(pk=1)
dir(user)
>>> class Test:
a = 5
>>> globals()['Test']()
<Test object at 0x7f73aade9cc0>
a = b[:]
# или
a = b.copy()
b = [1, 2]
>>> b.append(b)
>>> b
[1, 2, [...]]
>>> id(b)
139920848119752
>>> id(b[2])
139920848119752
>>> a = b[:]
>>> a
[1, 2, [1, 2, [...]]]
>>> id(a)
139920848120456
>>> id(a[2])
139920848119752
from copy import deepcopy
>>> a = deepcopy(b)
>>> a
[1, 2, [...]]
>>> id(a)
139920847744840
>>> id(a[2])
139920847744840
Если пытаюсь сразу в TextField вставлять текст с html-разметки, получается вот такая штука:
сategories = Category.objects.filter(
product__price__gte=100
).prefetch_related(
Prefetch('product_set', queryset=Product.objects.filter(price__gte=100))
).distinct('id')
for category in сategories:
products = category.product_set.all()
print(category.name, len(products), products)
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
t = Tiket.objects.filter(user=request.user, sost=True)
t = Tiket.objects.filter(user=request.user, sost=True).values()
return JsonResponse({'tiketsS': list(t)})
IP пользователя, URL, HTTP код ответа.
def previous_and_next(some_iterable):
prevs, items, nexts = tee(some_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return izip(prevs, items, nexts)
globals()['b'] = 2
print(b)
d = {}
d['b'] = b
print(d['b'])
SET foreign_key_checks=0;
... SQL import statements ...
SET foreign_key_checks=1;