These are chat archives for django/django

7th
Jun 2016
Andrew Backer
@awbacker
Jun 07 2016 05:13
I have a query I am trying to write purely in terms of the ORM, but I think my google-fu is failing me and I can't figure out how. Can anyone point me to the right docs/etc or a blog post covering this? I'm trying to replace the RawSQL:
Product.objects.filter(org=user_org)
    related_count=RawSQL(
        # product id is a reference to the outer "Product" instance, which is fully qualified in the query
        "SELECT count(*) FROM ta _ta INNER JOIN tb _tb ON _ta.b_id = _tb.id WHERE _tb.product_id = product.id",
        params=(),
    )
)
Andrew Backer
@awbacker
Jun 07 2016 05:27
I can't help but thinking there is a better way. Not even quite sure how to describe it.
Dmitry Fedotkin
@BlackPie
Jun 07 2016 06:07
Hi everyone. Can anyone explain me how to work with process_view() method in middlewares in Django 1.8? i have worked with middlewares but i cant make process_view() execute at all. https://gist.github.com/BlackPie/b81220f0d06e6698e36d6b38959c9684
mpachas
@mpachas
Jun 07 2016 06:19
@awbacker ta.objects.filter()
@awbacker sorry, something like this?
ta.objects.filter(bproductid=product.id).count()
ta.objects.filter(b__product__id=product.id).count()
Andrew Backer
@awbacker
Jun 07 2016 06:33
@mpachas Would that work as an annotation? The real query is a little more complicated than that, with select_related and a few wrinkles.
Dmitry Fedotkin
@BlackPie
Jun 07 2016 06:33
it turned out that django debug toolbar blocks all middleware process_view() methods
Robin Papa
@robinpapa
Jun 07 2016 06:34
Can some one help me with some database relationship modelling? What I would like to do is: have multiple "sea containers", a << container >> model , give each one a list of checkboxes (checkbox) as defined in the << checkbox >> model, and have a << status >> for those << checkboxes >> for each << container >>. Any one know how to do that?
Andrew Backer
@awbacker
Jun 07 2016 06:34
@BlackPie django-debug-toolbar/django-debug-toolbar#497
yeah, not much fun. I thought you could move it to the end and then it would be ok, and the docs confirm that.
Robin Papa
@robinpapa
Jun 07 2016 06:36

My current model setup is visible at http://stackoverflow.com/questions/37662521/how-to-correctly-use-template-tags-in-this-specific-many-to-many-relationship-in

But now I can iterate through checkboxstatus, but I want to iterate through containers.

Andrew Backer
@awbacker
Jun 07 2016 06:42
@mpachas I tried related_count=ta.objects.filter(tb__product_id=F("id")).count() and several variations, e.g. with Count and so on, to no luck
mpachas
@mpachas
Jun 07 2016 06:55
@awbacker there is an annotate() clause, http://docs.djangoproject.com/en/1.9/topics/db/aggregation/
Andrew Backer
@awbacker
Jun 07 2016 06:56
@mpachas Oops, I left that out of the snippet somehow. WIthout annotate the example I gave wouldn't work at all! crap
Fixed snippet:
Product.objects.filter(org=user_org).annotate(
        related_count=RawSQL(
            # product id is a reference to the outer "Product" instance, which is fully qualified in the query
            "SELECT count(*) FROM ta _ta INNER JOIN tb _tb ON _ta.b_id = _tb.id WHERE _tb.product_id = product.id",
            params=(),
        )
    )
)
Andrew Backer
@awbacker
Jun 07 2016 07:24
I was able to do it with Count("ta__tb") after I enabled the reverse relationship, but that is significantly slower and it's not a relationship I want to make visible. Sounds like a SO question
Mořeplavec
@moreplavec
Jun 07 2016 17:55
Hello, is it possible to do some basic math in Template? Something like: {{ myvalue.number/60 }}?
i found something like {{ myvalue.number|div:60 }} but it doesnt work too :/
Filip Figiel
@megapctr
Jun 07 2016 19:25
@moreplavec that stuff goes into your view code
Mořeplavec
@moreplavec
Jun 07 2016 20:23
hmm, i found solution: {% widthratio myvalue.number 60 1 %}
Taras
@ihappycoder
Jun 07 2016 20:25
Hello there, I’m trying to run django app on digital ocean with gunicorn and nginx using this https://goo.gl/VR88hb tutorial, everything done like there, but when I’m trying to run gunicorn it gives me an error "start: Job failed to start”, maybe anyone has the similar problem?
Patrick Harding
@senorpatricio
Jun 07 2016 20:29
@ihappycoder also digitalocean has great support and can help you deploy
Taras
@ihappycoder
Jun 07 2016 20:30
@cassiobotaro @senorpatricio thank’s will try it
Nathan Nichols
@nqthqn
Jun 07 2016 22:21

I've got the following fixture:

- model: auth.user
  pk: 2
  fields:
    username: p2
    first_name: Layla
    last_name: Quijas
    email: vatmaking@glyptical.com
    password: 123

But after I loaddatawhen I attempt to log in as "p2" it won't log me in. in admin it says "
Invalid password format or unknown hashing algorithm."

@senorpatricio thanks for your help earlier - I redid my virtual env, reinstalled everything and apache restarted and connected via WSGI successfully
Patrick Harding
@senorpatricio
Jun 07 2016 22:40
@natsn good to hear.
Filip Figiel
@megapctr
Jun 07 2016 23:10
@natsn thats cuz passwords are hashed before storing in the db
and fixture represents db state
Nathan Nichols
@nqthqn
Jun 07 2016 23:10
@megapctr I figured it out buy creating a user in the shell and then copying the hash from the password
I hope that is the correct method
Filip Figiel
@megapctr
Jun 07 2016 23:11
not really :O
Nathan Nichols
@nqthqn
Jun 07 2016 23:11
well, it worked fine
Filip Figiel
@megapctr
Jun 07 2016 23:11
what's the problem you want to solve?
Nathan Nichols
@nqthqn
Jun 07 2016 23:11
To create 15 interlinked profiles and user accounts in various states
using fixtures
this makes it simple to test various things
Filip Figiel
@megapctr
Jun 07 2016 23:12
what forces you to use fixtures?
Nathan Nichols
@nqthqn
Jun 07 2016 23:12
nothing - I could use a python script I suppose to create the accounts
Filip Figiel
@megapctr
Jun 07 2016 23:12
i see
you could take a look at factory boy library
lets you create models easily
Nathan Nichols
@nqthqn
Jun 07 2016 23:13
I have a 1000 line .yaml file with other dummy data already, figured user accounts could go in there too
Filip Figiel
@megapctr
Jun 07 2016 23:13
I see you're already commited to use fixtures then
Nathan Nichols
@nqthqn
Jun 07 2016 23:13
i guess, haha
Filip Figiel
@megapctr
Jun 07 2016 23:13
in this case, maybe storing the hash would be a better solution after all
because you don't have two different ways of providing test data
Nathan Nichols
@nqthqn
Jun 07 2016 23:14
what benefits does the factory offer though?
I've heard of "factories" but never understood how they differ from "fixtures" or "mock data"
Filip Figiel
@megapctr
Jun 07 2016 23:14
well, you don't need thousands of lines of yaml with factories
Nathan Nichols
@nqthqn
Jun 07 2016 23:15
So, how do I model hundreds of records without all those lines? seems like the most straight forward apprach
do factories happen dynamically or something?
yep
scroll a bit down to see an example
one nice thing is that you can provide generators for required fields
Nathan Nichols
@nqthqn
Jun 07 2016 23:17
huh, that's cool :)
Filip Figiel
@megapctr
Jun 07 2016 23:17
more often than not, you want to just have some instance of your model, and don't care about its fields - except for one or few fields you need for test
e.g. if you want to test age validation, you would use UserFactory(age=17)
and even though username is a required field, the factory can generate a username like user1
you need to configure this behavior though
Nathan Nichols
@nqthqn
Jun 07 2016 23:18
seems like it's more helpful for automatted tests
Filip Figiel
@megapctr
Jun 07 2016 23:19
yeah, a lot
I'll be honest with you, a project I started few months ago doesn't use factories
and I just realized how many lines of dumb code is there in my tests :worried: