SELECT reltuples FROM pg_class WHERE oid = 'schema_name.table_name'::regclass;
SELECT CASE WHEN some_field~E'^\\d+$' THEN some_field::integer ELSE 0 END FROM some_table;
CREATE OR REPLACE FUNCTION cast_to_int(text) RETURNS integer AS $$
BEGIN
RETURN CAST($1::varchar AS integer);
EXCEPTION
WHEN invalid_text_representation THEN
RETURN 0;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
CREATE CAST (text as integer) WITH FUNCTION cast_to_int(text);
SELECT CAST('asd'::text as integer)
вернёт 0 from PIL import Image
img = Image.open("img.jpg")
w5 = (img.size[0] // 100) * 5
h5 = (img.size[1] // 100) * 5
croped = img.crop(
(
w5,
h5,
img.size[0] - w5,
img.size[1] - h5
)
)
croped.save("croped.jpg")
from PIL import Image
from PIL import ImageOps
img = Image.open("img.jpg")
w5 = (img.size[0] // 100) * 5
croped = ImageOps.crop(img, w5)
croped.save("croped.jpg")
class Wish(models.Model):
customer = models.ForeignKey(User)
item = models.ForeignKey(Product)
class Meta:
unique_together = ['customer', 'item']
class WishCreate(CreateView):
model = Wish
fields = ['item']
def form_valid(self, form):
obj = form.save(commit=False)
obj.customer = self.request.user
obj.save()
return http.HttpResponseRedirect(self.get_success_url())
class WishDelete(DeleteView):
model = Wish
success_url = reverse_lazy('wish-list')
class WishList(ListView):
model = Wish
def get_queryset(self):
return Wish.objects.filter(customer=self.request.user)
select employee_id, sum(work_hours) as work_hours from (
select employee_id, datediff(hh, coming_time, leaving_time) as work_hours
from work_schedule
where Year(coming_time) = Year(CURRENT_TIMESTAMP) and Month(coming_time) = Month(CURRENT_TIMESTAMP)
) as sq group by employee_id;
class Category(MPTTModel):
parent = TreeForeignKey('self', verbose_name=u'Родитель', null=True, blank=True, related_name='children')
name = models.CharField(u'Название', max_length=100)
class Product(models.Model):
category = TreeForeignKey(Category, verbose_name=u'Категория')
...
SELECT name FROM catalog_category AS cc
INNER JOIN LATERAL (
SELECT cc.id AS id, SUM(products) AS p_count FROM (
SELECT cc.id AS parent_id, category_id, COUNT(id) AS products
FROM catalog_product
WHERE category_id IN (
SELECT id FROM catalog_category
WHERE lft <= cc.rght AND lft >= cc.lft AND tree_id = cc.tree_id)
GROUP BY category_id
) AS sub_cс
GROUP BY parent_id
) AS cp
USING(id) WHERE cp.p_count > 0;