I'd like to save a WSGIRequest to a Django model to do some debugging. Is it possible to create a model to do this? I get all kinds of errors when I try:
class MyRequest(models.Model):
request = WSGIRequest()
Thanks in advance.
Why would you expect to be able to persist an object with all kinds of ephermal state to the database?
You best bet is probably to write a function that takes a Request object, pulls out whatever information is relevant to you and creates a string with the info in persist that.
Doing a json.dumps on the whole object work.
Related
Some variables (class object) needs to be shared among different view functions. Each view function changes this object's properties. This object is user-specific, that is to say, user needs to log in the web page, then access different webpage. I do not want different users share/change this object variable. What I can think of is:
Using global to declare a variable in different view functions. However, in it is running in Django runserver with multi-threaded, when multiple users access it, will the global variables be changed/shared by all users? or this variable is specific to each user?
Using session, however, the variable is object, which is not json serializable.
Store it in database, however, this is object and it is not allowed to store in the database. I cannot pickle it either.
What is the correct way of sharing variables among different view functions while allowing multiple users access server concurrently?
Thanks,
This is the problem I met. Please just explain a little bit, please do not just down vote without any comment, thank you
You say the object is user-specific. Is it also session-specific? If the user were to open a new tab in his browser and access another page in your app, could he have two different objects? If so, then it makes sense to store it in the session. You can use pickle serialization to get around JSON (although it can lead to insecurities, if the object is user-provided.)
I have newfies dialer project, i would like to add a new field in Call Reports section.
I have lot of field that are unused in contact model. One of them can be used for this special id for my purpose.
How can i do that? Please help me anyone who familiar with newfies-dialer.
Newfies-Dialer is based on the Django framework, so it's good to know about Django to hack on the project.
You will notice in Newfies-Dialer that there is a template called: dialer_cdr/templates/dialer_cdr/voipcall_report.html
in which we display the data that is passed from the view.
So then in dialer_cdr/views.py, you have a view function which is in charge of rendering template and pass it some data. There, you can either modify voipcall_list object to add extra data to it, like info from the contact model, or pass an other object to data.
Here a link to the function handling this view: https://github.com/Star2Billing/newfies-dialer/blob/v2.12.2/newfies/dialer_cdr/views.py#L169
Am on Django 1.6
In my app I'd like to have a User object and a separate profile object. I'd like to avoid implementing a custom User object and instead just use a one-to-one relation to a model that has all the additional fields/info I want.
Whenever I do a query on a User object, 90% of the time I will want to retrieve this profile object. My thinking is that the best way to do this is to modify the get_queryset method of the default UserManager to always retrieve this related object via a "select_related" call.
Is there a way to do this?
Searching around this is the only resource i could find on the topic.
Override Django User Manager to only return active users in queries
Do I need to do it this way? Can I instead just somehow use the base User object?
Thanks in advance.
Since Django 1.5 you can create your custom user model. This eliminates the need of user_profile.
Question
In Django, when using data from an API (that doesn't need to be saved to the database) in a view, is there reason to prefer one of the following:
Convert API data (json) to a json dictionary and pass to the template
Convert API data (json) to the appropriate model object from models.py and then pass that to the template
What I've Considered So Far
Performance: I timed both approaches and averaged them over 25 iterations. Converting the API response to a model object was slower by approximately 50ms (0.4117 vs. 0.4583 seconds, +11%). This did not include timing rendering.
Not saving this data to the database does prevent me from creating many-to-many relationships with the API's data (must save an object before adding M2M relationships), however, I want the API to act as the store for this data, not my app
DRY: If I find myself using this API data in multiple views, I may find convenience in putting all my consumption/cleaning/etc. code in the appropriate object __init__ in models.
Thanks very much in advance.
Converting this to a model objects doesn't require storing it in database.
Also if you are sure you don't want to store it maybe placing it in models.py and making it Django models is wrong idea. Probably it should be just normal Python classes e.g. in resources.py or something like that, not to mistake it with models. I prefer such way because maybe converting is slower (very tiny) but it allows to add not only custom constructor but others methods and properties as well which is very helpful. It also is just convenient and organizes stuff when you use normal classes and objects.
Pass the list of dictionaries directly to the template. When you need to use it with models, use .values() to get a list of dictionaries from it instead.
Quick question. In my syndication feed framework code,
http://docs.djangoproject.com/en/dev/ref/contrib/syndication/
what is the best way to get access to the session? I don't have
access to the request, and I can't use
from django.contrib.sessions.backends.db import SessionStore
as I don't know the session ID, but I need to access some of the
variables in the session.
i.e. I have:
from django.contrib.syndication.feeds import Feed
class LatestPhotos(Feed):
...
and in that LatestPhotos class, I need to access something in the session to help control the logic flow. I can't find any documentation on the best way to do it.
Thanks
Thanks!
It seems like a design flaw to be trying to access session data in the LatestPhoto's class. I would assume that if your syndication feed depended on a session variable, then the items you're syndicating (LatestPhotos) should be constructed with that variable?
Can you make the logic flow decision before you construct the LatestPhotos object, or at the very least pass the session ID in to the LatestPhotos init routine?
Figured it out - drrr, so simple. The syndication framework Feed class has a member called request...so simple I never thought of it :)
[this comment applies to django 1.1 and earlier syndication framework]