Maluco Marinero posted:You could try using manage.py dumpdata. That dumps all your data into JSON files that can then run into manage.py loaddata. Glad I asked, that looks like exactly what I was looking for Thanks Maluco! e: quoted since we're on a new page
|
|
# ? Nov 3, 2016 00:49 |
|
|
# ? May 15, 2024 04:09 |
|
I'm fairly new to Django and playing around with it so this might be the most idiotic implementation, but I've got a models field question. I'm looking to enumerate through a list of files on the server to be displayed as shortcuts on a template page dynamically with some of their file information (for example, log files); here's what I have for models.py:code:
I've tried manually specifying a path for the field in the model as well e.g. (path='/logs/'), but the field in the admin pane stays blank, can't be typed into and nothing happens when the dropdown is clicked; I can't add an item because the field is required. Should I just be using a normal CharField instead of FilePathField? The info I've found on it from searching online and looking at the fields documentation makes it sound like FilePathField is what I would want for that property, but maybe I'm missing a setup step? Otherwise finding Django to be pretty slick; I've been working through the CodeSchool courses on it and Python recently and am really enjoying how much it simplifies things. Mo_Steel fucked around with this message at 06:40 on Nov 6, 2016 |
# ? Nov 6, 2016 06:34 |
|
Two things, the path must be absolute according to the docs, not relative to the working directory, secondly, I'd check your permissions, as Django may just silently fail to get anything if it doesn't actually have permission to read the path. Reading more, I've never used but the path is a necessary argument. You're not allowed to do your own thing with it, it needs a path to work from, and in the admin it only allows you to choose what it finds. I imagine this is so it can simplify accessing the file referenced.
|
# ? Nov 6, 2016 08:09 |
|
Maluco Marinero posted:Two things, the path must be absolute according to the docs, not relative to the working directory, secondly, I'd check your permissions, as Django may just silently fail to get anything if it doesn't actually have permission to read the path. Yeah, now that I see it in action I understand a bit more: I was able to get that by pointing the path at settings.BASE_DIR; I think my confusion was I read the documentation as a relative path. I'm on Windows 10, and you can't drop in "C:\files" because the backslashes prevent makemigrations from completing. I also tried "file:///C:/files" which allows migration but doesn't show anything in the field. I assume I'd probably have to do something like how BASE_DIR is defined: os.path.dirname(os.path.dirname(os.path.abspath("C:\files"))) maybe. Anyway, it looks like this would be more useful if I was looking to let users upload files and specify a directory to put them in from a set of folders as it doesn't show the full path; I'll keep tinkering around and see if CharField isn't more what I'm looking for by just populating it with the name of the folder containing each existing file. Thanks for the help! Mo_Steel fucked around with this message at 16:15 on Nov 6, 2016 |
# ? Nov 6, 2016 16:12 |
|
Has anyone else found that over their Django career they migrated back to function-based views from class-based views? I used to use CBV's quite extensively, but I've found that I just really value the explicit nature of FBV's and that they seem to better mesh with the "explicit is better than implicit" Python mantra. It's not that I really find CBV's to be all that bad, I've just slowly grown into this mindset where I find them unnecessary.
|
# ? Nov 8, 2016 19:40 |
|
Thermopyle posted:Has anyone else found that over their Django career they migrated back to function-based views from class-based views? I think the best-case for CBVs are a fairly slim set of scenarios (massive derivative CRUDs) and add unnecessary complexity to an app. I don't think I've ever set up a series of views and then later ever considered refactoring them into CBVs for any benefit whatsoever. But I have created some CBVs or worked with CBVs where later I thought to myself, "WTF is this? what plugs into this?" and spent some effort refactoring into FBVs.
|
# ? Nov 8, 2016 19:44 |
|
Not that I have much of a Django career since it's a hobby for me, but I hate CBVs. Having so much hidden functionality makes it extremely hard to know what's happening and why, or what specific function you need to overload to get it to work how you want. After all that it feels like fighting it more than working with it.
|
# ? Nov 8, 2016 19:51 |
|
porksmash posted:Not that I have much of a Django career since it's a hobby for me, but I hate CBVs. Having so much hidden functionality makes it extremely hard to know what's happening and why, or what specific function you need to overload to get it to work how you want. After all that it feels like fighting it more than working with it. I agree with all of this, but just in case you don't know about it, this site helps to a degree: https://ccbv.co.uk/
|
# ? Nov 8, 2016 19:54 |
Thermopyle posted:Has anyone else found that over their Django career they migrated back to function-based views from class-based views? drat I'm glad you said this. I always feel like I'm doing something dirty by wiring up views one-to-one with URLs and building explicit dicts for my return objects, but six months later if I have to tinker with something it makes it a billion times easier. And what if your app does more than basic CRUD, where oh I dunno you have like three or four different kinds of mutate actions that operate on an object (or several different objects that aren't all that separable) instead of just one obvious "update"? CBVs seem like a case of designing around the happy path and then trimming out all the flexibility to do non-happy-path things. I don't think anyone's first exposure to DRF serializers and ViewsSets should be when they have to reverse-engineer someone else's beautiful perfect CRUD API to jimmy in a new piece of functionality that doesn't fit quite so neatly into that pattern, meaning they have to rip the whole thing apart to expose all the nuts and bolts they need.
|
|
# ? Nov 8, 2016 20:22 |
|
Is there a more graceful way to handle enumerated values than what I am doing now? I am finding it very tedious to work with them with the Django REST Framework. I started out with Python 2.7, but I have been able to since move up to Python 3.5, so I have Enums now officially. I wonder if Django has some built-ins to exploit. The model has stuff like this: code:
code:
...It has occurred to me while vomiting this out that maybe what I want is ChoiceField in my model. Yes? Edit: Oh hey, should I be looking at django-enumfield and drf-enumfield instead? Rocko Bonaparte fucked around with this message at 00:34 on Nov 9, 2016 |
# ? Nov 9, 2016 00:00 |
|
Thermopyle posted:Has anyone else found that over their Django career they migrated back to function-based views from class-based views? Absolutely, I much prefer function and composition/decorators to class based views after a brief fling with them early on.
|
# ? Nov 9, 2016 23:26 |
|
Thermopyle posted:Has anyone else found that over their Django career they migrated back to function-based views from class-based views? I still use CBVs whenever I build things with Django but that has become very rare since I started teaching full time. Honestly, though, I don't really care one way or the other. Views are views.
|
# ? Nov 10, 2016 00:12 |
|
I used the excuse of having to change hosting providers to start porting all my sites to use Python 3 instead of 2 and get them all running on the latest version of Django and so far everything has gone very smoothly with one exception. I can't for the life of me find an openid library that supports Python 3 and is up to date enough to not call a bunch of deprecated/removed django functions. Does anybody know of one that's up to date (for a consumer only, specifically logging in to a site via Steam)
|
# ? Nov 11, 2016 21:17 |
Dr. Dos posted:I used the excuse of having to change hosting providers to start porting all my sites to use Python 3 instead of 2 and get them all running on the latest version of Django and so far everything has gone very smoothly with one exception. I use python-social-auth with Python 3 and the latest version of Django & Django Rest Framework. It "just works" and I have never had to fiddle with it.
|
|
# ? Nov 12, 2016 01:47 |
Where do you guys put your signals code and how do you make sure it gets imported? I want to stick mine in a separate signals.py file and came across this solution but wasn't sure if that's the right way to go
|
|
# ? Nov 16, 2016 20:50 |
|
fletcher posted:Where do you guys put your signals code and how do you make sure it gets imported? That's the official solution, AFAIK. One of my students was trying to use that for their own project, though, and having some problems, so proceed with caution?
|
# ? Nov 16, 2016 21:50 |
|
fletcher posted:Where do you guys put your signals code and how do you make sure it gets imported? Yeah, this is the way to go. Make sure you follow all the steps! I often forget to put the default_app_config part in app/__init__.py
|
# ? Nov 17, 2016 01:45 |
Thermopyle posted:Yeah, this is the way to go. Apparently they tell you to avoid setting default_app_config now, and instead just list it in INSTALLED_APPLICATIONS: https://docs.djangoproject.com/en/1.9/ref/applications/#configuring-applications It seems to all be working now...just seems a little hacky And my signals are definitely being registered multiple times now (as warned in the docs), fortunately they are idempotent though.
|
|
# ? Nov 17, 2016 02:12 |
|
I'm trying to figure out how to handle some stuff with the Django REST Framework. I have a master record format in one model that is made up of other objects in other models. I would like to create a new one of these, but I want to override how this is done. In particular, I want the creation API to take specific terms for looking up the other objects that make up the record, rather than the user having to vomit all that raw information in their request. Here's what I understand I have to do: 1. Override create() in my serializer for this master record object that I first created when I got it working in the REST framework. 2. Do my massaging to get the actual objects based on the custom inputs. 3. Have that massaged data then get pumped into MyMasterRecord.objects.create_method_i_wrote(properties=this,other_properties=that...) 4. Add a models.Manager subclass that describes create_method_i_wrote. I don't see how this connects anywhere. I just kind of declared the class and the method and just assume elf magic hooks it up. I figured that in my create() method that I could take the validated JSON and do all the lookups. This appears to be false. Heck, my unit test doesn't even run any of the above code. It's puking out because it's claiming that two of my fields are missing. They are definitely there, but their models are objects themselves with compound data, rather than the simple strings I'm passing in. I figure I'd be able to retrieve those strings, derive a lookup based on them, and transform them into the actual objects I want. So do I have to do something like write my own validator for the create requests? Is there a better way to get this stuff? Can I add stuff to all these other Models that let me recall them based on some criteria other than their primary key?
|
# ? Nov 22, 2016 01:39 |
|
Rocko Bonaparte posted:I'm trying to figure out how to handle some stuff with the Django REST Framework. I have a master record format in one model that is made up of other objects in other models. I would like to create a new one of these, but I want to override how this is done. In particular, I want the creation API to take specific terms for looking up the other objects that make up the record, rather than the user having to vomit all that raw information in their request. Here's what I understand I have to do: The time you spend figuring out how to override and dig into the 'magic' inside the framework, you could just do everything manually with plain old Django models then serialize/deserialize the set separately. I mean you could rig the framework to do what you want, spend some time, pat yourself on the back for a clever solution with some complex inheritance/mixin thing, but the things you learn are pretty specific to how one guy made this one framework and not transferrable skills.
|
# ? Nov 23, 2016 16:37 |
|
Here's something I do that I always feel "iffy" about.... For simplicity sake say I've got a todo app site. Users come along and register their own accounts. My TodoList and ToDo models have a `user` FK field to the Django User model. In my views and forms I need to check request.user against object.user and make sure not only that the user owns the object at the current url, but also that I'm filtering the queryset for various related fields on the forms on that page so that when the user selected a related object in those form fields they're only able to select objects that they own. On the model side I need to make sure that only objects with the same user can be saved as related. This all requires a little bit of code in a lot of places. I can simplify it via the use of custom mixins or decorators, but I've still got to remember to use them. I try to use tests to make sure these constraints are met. What other ways of approaching this problem do you like to use? Is there a way to force a constraint on user must equal user at the DB level for these related objects?
|
# ? Nov 25, 2016 20:56 |
|
You can use a per-object permissions package like django-guardian
|
# ? Nov 26, 2016 20:54 |
|
Thermopyle posted:Here's something I do that I always feel "iffy" about.... Depends on your model design. It's pretty simple if you keep things normalized and then manage single reusable abstracted entry points for querying/saving/updating. Other alternatives includes overriding the save methods and ensuring there any save call is done by a related /owning user.
|
# ? Nov 28, 2016 18:28 |
|
I'm having some trouble with inline formsets. I have a parent-child (one-to-many) relationship. I want a single page where I can edit the properties of an parent, while also adding/editing/deleting the children. Displaying a parent along with it's children works fine. However when I save, I get the error quote:ManagementForm data is missing or has been tampered with Edit: The problem was my template was missing the 'header' part of the formset: HTML code:
https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#understanding-the-managementform epswing fucked around with this message at 19:13 on Nov 29, 2016 |
# ? Nov 28, 2016 19:34 |
|
What the best way these days for a Django app to generate PDF files? The docs point to ReportLab, but after a couple days I'm finding it hard to work with, and I find the ReportLab documentation pretty awful. There's the django-wkhtmltopdf wrapper around wkhtmltopdf, but requiring a 40mb dependency doesn't make me feel warm and fuzzy the way a django app should. There's django-easy-pdf but github readme says it depends on xhtml2pdf and reportlab. I'm not seeing any obviously good options. Any suggestions? What do you use? epswing fucked around with this message at 19:15 on Nov 29, 2016 |
# ? Nov 28, 2016 21:35 |
|
I was having a great time with my project until I started dealing with user accounts. The code size has doubled overall, although a lot of that is on the front end. I am trying to figure out the mechanics behind it on the back end. Say, I am particularly trying to get Angular to handle logging into my REST API c/o the Django REST Framework. In particular, I see a few examples like this: https://richardtier.com/2014/03/15/authenticate-using-django-rest-framework-endpoint-and-angularjs/amp/ Ignoring the JavaScript for a bit--although I am having my fair share of trouble interpreting what tha is doing--what is that APIView trying to do? This and another site just casually declare a UserSerializer, which is only a thing in the Django REST Framework tutorials. Maybe I keep missing mention of that in these articles. I have my own that tacks on some data, sure, but it is working on full User Models. I guess I look the User up and serialize that instead? In particular, how would I unit test such a thing? If I use APIClient and post to the login URL with my authentication data, will that preserve my credentials for the next post afterwards? This is where me being new to web stuff really gets me into trouble.
|
# ? Dec 13, 2016 05:22 |
|
My favorite way to do user stuff when building a REST API with a JavaScript front-end is JWT. There's even a nice Django package for doing it. Google Django JWT.
|
# ? Dec 13, 2016 15:23 |
|
EDIT: Ignore everything. Apparently soft reloading wsgi instead of fully restarting caused some sort of issue. I've run into some issues with the logging configuration on my server. I wanted to make sure I understand how to see what exactly Django thinks the logging config is. According to the docs, there's a default logging configuration that gets merged with a LOGGING dict specified in your settings, and this is what is used while Django is running. I am fine with the default logging setup, so I have not specified anything in my settings.py. There is no LOGGING = xxx or LOGGING_CONFIG = xxxx in my settings at all. I'm not really sure how to find what exactly the final configuration for logging Django is using, though. It's apparently not using the supposed defaults. I know my email settings are correct because I receive emails sent with send_mail() or mail_admins(). I'm basically at a loss here because I can't tell what's going on internally. porksmash fucked around with this message at 21:44 on Dec 14, 2016 |
# ? Dec 14, 2016 20:29 |
|
Rocko Bonaparte posted:
Are you asking about how to unit test controllers that require some level of authentication/authorization? In my unit tests I use force_authenticate when I'm not testing the login controller itself.
|
# ? Dec 17, 2016 16:33 |
|
WINNINGHARD posted:Are you asking about how to unit test controllers that require some level of authentication/authorization? In my unit tests I use force_authenticate when I'm not testing the login controller itself. In this case, I would want to actually test that the mechanics of authentication worked. It looks like I got it. I have a test that will 403 on a POST after logging off.
|
# ? Dec 24, 2016 05:46 |
|
If you set null=True and default=0 on the same field, is the null=True setting ignored? I had written some of my optional fields with "blank=True, null=True, default=0", so I was going back through them and changing them.
|
# ? Dec 30, 2016 11:50 |
|
galenanorth posted:If you set null=True and default=0 on the same field, is the null=True setting ignored? I had written some of my optional fields with "blank=True, null=True, default=0", so I was going back through them and changing them. Not ignored, it would let you set the field to null, either explicitly during creation or on a later update.
|
# ? Dec 30, 2016 14:51 |
|
Anyone used more than one of the various django social auth packages and have anything to say about which they like the most?
|
# ? Jan 1, 2017 20:04 |
|
Thermopyle posted:Anyone used more than one of the various django social auth packages and have anything to say about which they like the most? I'm interested in this as well, as I'll soon be integrating with Facebook login. There seem to be several deprecated packages out there, some more popular than others.
|
# ? Jan 2, 2017 02:35 |
Got a rather interesting problem here. I got this model, see. I want the id to be a BigAutoField, and I want it to be a randomly generated integer, not sequential. Purpose being that I don't want it to be easily guessable or predictable. And because I'm going to be using a Base## method to shorten it for URL exposure, I don't want the ID to be a long or alphanumeric thing like a UUID. So I'm doing this: code:
If I do something like: code:
I feel like I'm barking up the wrong tree here. Any ideas on what I ought to be doing? Edit: looks like this might get me past it; leaving it here in case it helps anybody: http://stackoverflow.com/questions/21458387/transactionmanagementerror-you-cant-execute-queries-until-the-end-of-the-atom Data Graham fucked around with this message at 06:31 on Jan 2, 2017 |
|
# ? Jan 2, 2017 06:24 |
|
Thermopyle posted:Anyone used more than one of the various django social auth packages and have anything to say about which they like the most? The worst part of it is the sheer number of templates it uses. If you need to add translations or customize the overall layout or the like then you're looking at copy-pasting and modifying almost two dozen HTML files. Aside from that I'd still recommend it as it was the most comprehensive and, outside of editing so many templates, I was able to get social auth integrated in with minimal fuss to my backend code.
|
# ? Jan 2, 2017 17:24 |
|
IAmKale posted:I used django-allauth recently on a project and recommend it as the best social auth plugin. It supports almost any social auth provider you can think of, and you can configure it via the Django admin panel. I agree with this post (the good and the bad...)
|
# ? Jan 4, 2017 05:18 |
|
I just came across intercooler.js, which Django users might be interested in. Sometimes you want to do AJAX-y stuff but it can be a lot of trouble to wire up django-rest-framework. This thing uses attributes on elements to hit your server and replaces those elements with the HTML it gets back. So, you can just do something like this in one of your templates: code:
So...dynamic, AJAX-y pages with just regular templates.
|
# ? Jan 13, 2017 17:21 |
Thermopyle posted:I just came across intercooler.js, which Django users might be interested in. This is pretty cool, thanks for sharing! Kinda reminds me of JSF
|
|
# ? Jan 18, 2017 19:43 |
|
|
# ? May 15, 2024 04:09 |
|
Hey dudes: How do I access files (like a pdf or w/e) from code in a django app on Heroku? I've been pulling my hair out regarding this!! -Works locally -When I clone, the file's there, so it is getting pushed to Heroku -Static files like Javascript and css work fine; dj-static configured. I've tried dumping the file I need the code to access everywhere; main proj and app static, staticfiles directories, the main proj directory, the main app directory. Nothing, and no leads on troubleshooting since it's only reproducible when deployed. You should see my commit log! I get a standard FileNotFound Error. Based on my weak understanding of serving static files, this is one one in the trad sense; it's accessed by the code, not the user. edit: Got it working, after dropping the file in about ~10 locations simultaneously; no idea which one was correct! Dominoes fucked around with this message at 11:50 on Feb 6, 2017 |
# ? Feb 6, 2017 11:00 |