These are chat archives for django/django

20th
Jan 2016
kdelinx
@kdelinx
Jan 20 2016 04:36

Hi everybody! I'm working with Django REST and second day i'm dummy...
I need output some list, but it not correctly:

    @staticmethod
    def get_product(obj):
        lang = get_language()[:2]
        list_access = []
        products = obj.product.model.objects.language(lang).\
            filter(is_published=True,
                cartproductcustom__product__in=[cache_prod.id for cache_prod in
                    obj._prefetched_objects_cache['product']]).\
            prefetch_related('accessories').distinct()
        for product in products:
            accessories = product.accessories.model.objects.language(lang).\
                filter(accessories__in=[acs for acs in product.
                       accessories.all()]).\
                values('title', 'price', 'desc', 'images', 'incompare',
                       'number_code', 'recommended', 'id',).distinct()
            for access in accessories:
                list_access.append({'id': access['id'],
                                    'desc': access['desc'],
                                    'title': access['title'],
                                    'price': access['price'],
                                    'incompare': access['incompare'],
                                    'number_code': access['number_code'],
                                    'recommended': access['recommended'],
                                    'images': get_preview(access['images']),
                                    })
            yield {'id': product.id, 'title': product.title,
                   'desc': product.desc, 'price': product.price,
                   'date_created': product.date_created,
                   'recommended': product.recommended,
                   'incompare': product.incompare,
                   'number': product.number_code,
                   'accessories': list_access if accessories else [],
                   }

list_access appending from full list of products, but i need append it one by one, what shoud i do?

Trương Tuấn Quang
@tuanquanghpvn
Jan 20 2016 07:03
Hello everyone :smile:
lego
@legobillyjoe
Jan 20 2016 09:28
@moviedo Thank you very much! That helped.
friendOfOurs
@friendOfOurs
Jan 20 2016 09:51

Are signals more elegant and preferred solution than:

user = userForm.save()
user_info = UserInfo(user=user)
user_info.save()

And why is the code above not so great? Thanks!!!

Filip Figiel
@megapctr
Jan 20 2016 10:00
@friendOfOurs is UserInfo.user an one-to-one field? then maybe use django-annoying library
it has a nice AutoOneToOneField
friendOfOurs
@friendOfOurs
Jan 20 2016 10:25
hey @megapctr, thanks for givining me a helping hand again :) yep it is one-to-one field, and i want to save user_info right after registration (creation of user)
it works this way, but I was thikning if with signals might be a nicer solution
Filip Figiel
@megapctr
Jan 20 2016 10:26
signals are nice too, but sometimes an explicit object creation is easier to read or debug
glad to help :)
friendOfOurs
@friendOfOurs
Jan 20 2016 10:45
so there is nothing bad with saving one model first and then using that saved model as a field value when saving the next
?
Filip Figiel
@megapctr
Jan 20 2016 10:46
of course not, but you may or may not want to handle it "manually"
depends if it's an important side-effect or a regular action that's naturally expected
for example, sending an e-mail may be preferred to happen explicitly. creating models probably not
friendOfOurs
@friendOfOurs
Jan 20 2016 11:06
and for important side-effects signals should be preferred?
like sending an email
Job Matheka
@jobm
Jan 20 2016 11:07
What is the best way to implement user permissions in Django??
Filip Figiel
@megapctr
Jan 20 2016 12:04
@friendOfOurs what I meant is you should use signals to less important stuff, and explicitly invoke more important things
@jobm django has a built-in permission system. I never got the hang of it, but perhaps it will work for you https://docs.djangoproject.com/en/1.9/topics/auth/default/#topic-authorization
phlax
@phlax
Jan 20 2016 16:30
hi - i have a migration to make certain columns in MySQL case in/sensitive by setting them either to utf8_bin or utf_general_ci - as per the django docs - the problem is if any alterations are made to those fields it resets the collation - any way to avoid this?