How to see what data is passed to the class? - python

I have a product model with a json field as product attributes. I want to make filters on all keys of this field. I am using Djnago-filter.
enter image description here
When I declare a field and process this request in the method, everything works.
Example:
tip = django_filters.CharFilter(method = 'filter_attrs')
"api/v1/product/?tip=Городской"
name = tip
in the "filter_attrs" method I get the name argument which is equal to the key in the filter. Its work.
But if I make such a "api/v1/product/?ves=30" request, the method is not even called.
So I want this method to process requests regardless of what is in the name argument.
I wanted to see inside the class what request comes in and in what case the method is called, and override this rule. But I don't understand how to do it.
Please help me how to learn how to do this so that in the future I can cope with such tasks on my own
I tried to call the "init" method inside which to call print(request,queryset), but this method was apparently not called, I did not see anything in the terminal.
I tore off the files filterset.py(djang0_filters/rest_framework/filterset.py) and tried to find methods in which you can try queries and override them. But didn't find

Related

Accessing objects with a foreign key relationship in Django?

I'm currently trying to modify the django-podcast module so the podcast's xml file is statically served instead of being generated on every request for it.
I am attempting to rewrite the channel's xml file every time an episode is modified, created, or deleted and to do so I'm using django signals. What I would like to do is something like this...
from django.db.models.signals import post_save, post_delete
from django.template.loader import render_to_string
def update_xml_file(sender, **kwargs):
f = open('channelrss.xml', 'w')
f.write(render_to_string('podcast/show_feed.html', {'object': sender.show}))
f.close()
class Show(models.Model):
...
class Episode(models.Model):
post_save.connect(update_xml_file)
post_delete.connect(update_xml_file)
...
show = models.ForeignKey(Show)
...
The problem I keep running into is that sender.show is a ReverseSingleRelatedObjectDescriptor and not an actual instance of the Show class. I also tried reloading the sender object using sender.pk as the primary key value like this...
Episode.objects.filter(pk=sender.pk)
but apparently sender.pk returns a property object and not an integer or string and I don't know how to get it's value, so I guess I have two questions.
How can I retrieve the instance of Show associated with the Episode? and what the heck is a property object and why does sender.pk return it?
Thanks ahead of time for your response!
Josh
You can try:
def update_xml_file(sender, instance=False, **kwargs):
f = open('channelrss.xml', 'w')
f.write(render_to_string('podcast/show_feed.html', {'object': instance.show}))
f.close()
when instance.show.name_field is name_field of the model.
I finally figured it out! This issue was due to my lack of knowledge on the arguments being sent to my signal handler.
The sender argument sent to my handler was actually a class object and not the instance itself. In order to retrieve the instance itself I needed to use kwargs['instance'] and in order to retrieve the Show instance I simply used kwargs['instance'].show
As a result I think I understand where the property object issue was coming from. Correct me if I'm wrong, but when trying to access a 'class' object instead of an 'instance of a class' object the properties aren't defined as string or integer values, but rather property objects that need to be defined.
Also as an additional note, the signal.connect() function doesn't need to be defined in the class's model and the way it is written above is somewhat deceiving. The way it's connected above will listen for any object's save or delete signal sent. In order to associate the function with signal's from only Episode objects I should have written it as...
post_save.connect(update_xml_file, sender=Episode)
post_delete.connect(update_xml_file, sender=Episode)
or by using a decorator as shown in Django's signal documentation.
Thanks again for all the help!
Josh

How to create instance of Django Field with Primary Key=True

Im working on a django application and I can't seem to get this one piece of code working. I know of some hacky ways to accomplish my task, but it seems like there should be a better way. I have a model class with one field in it:
class MobileUser(models.Model):
phone_id = models.AutoField(primary_key=True)
and everything is groovy. However, when I want to create an instance of the class and save it to the database like so:
mobile = MobileUser()
mobile.save()
I get a warning saying that MobileUser has no default value arguments, which makes sense. I know I can do things like get the last inserted row/the max id + 1 and pass that in as an argument but I'd prefer not to do that. Any ideas? Oh, and side note, the operation works it just throws a warning.
Why not let django handle it then, just do this:
class MobileUser(models.Model):
pass
Whenever you create an entry it would auto increment the id field.

Python: Assign an object's variable from a function (OpenERP)

I'm working on a OpenERP environment, but maybe my issue can be answered from a pure python perspective. What I'm trying to do is define a class whose "_columns" variable can be set from a function that returns the respective dictionary. So basically:
class repos_report(osv.osv):
_name = "repos.report"
_description = "Reposition"
_auto = False
def _get_dyna_cols(self):
ret = {}
cr = self.cr
cr.execute('Select ... From ...')
pass #<- Fill dictionary
return ret
_columns = _get_dyna_cols()
def init(self, cr):
pass #Other stuff here too, but I need to set my _columns before as per openerp
repos_report()
I have tried many ways, but these code reflects my basic need. When I execute my module for installation I get the following error.
TypeError: _get_dyna_cols() takes exactly 1 argument (0 given)
When defining the the _get_dyna_cols function I'm required to have self as first parameter (even before executing). Also, I need a reference to openerp's 'cr' cursor in order to query data to fill my _columns dictionary. So, how can I call this function so that it can be assigned to _columns? What parameter could I pass to this function?
From an OpenERP perspective, I guess I made my need quite clear. So any other approach suggested is also welcome.
From an OpenERP perspective, the right solution depends on what you're actually trying to do, and that's not quite clear from your description.
Usually the _columns definition of a model must be static, since it will be introspected by the ORM and (among other things) will result in the creation of corresponding database columns. You could set the _columns in the __init__ method (not init1) of your model, but that would not make much sense because the result must not change over time, (and it will only get called once when the model registry is initialized anyway).
Now there are a few exceptions to the "static columns" rules:
Function Fields
When you simply want to dynamically handle read/write operations on a virtual column, you can simply use a column of the fields.function type. It needs to emulate one of the other field types, but can do anything it wants with the data dynamically. Typical examples will store the data in other (real) columns after some pre-processing. There are hundreds of example in the official OpenERP modules.
Dynamic columns set
When you are developing a wizard model (a subclass of TransientModel, formerly osv_memory), you don't usually care about the database storage, and simply want to obtain some input from the user and take corresponding actions.
It is not uncommon in that case to need a completely dynamic set of columns, where the number and types of the columns may change every time the model is used. This can be achieved by overriding a few key API methods to simulate dynamic columns`:
fields_view_get is the API method that is called by the clients to obtain the definition of a view (form/tree/...) for the model.
fields_get is included in the result of fields_view_get but may be called separately, and returns a dict with the columns definition of the model.
search, read, write and create are called by the client in order to access and update record data, and should gracefully accept or return values for the columns that were defined in the result of fields_get
By overriding properly these methods, you can completely implement dynamic columns, but you will need to preserve the API behavior, and handle the persistence of the data (if any) yourself, in real static columns or in other models.
There are a few examples of such dynamic columns sets in the official addons, for example in the survey module that needs to simulate survey forms based on the definition of the survey campaign.
1 The init() method is only called when the model's module is installed or updated, in order to setup/update the database backend for this model. It relies on the _columns to do this.
When you write _columns = _get_dyna_cols() in the class body, that function call is made right there, in the class body, as Python is still parsing the class itself. At that point, your _get_dyn_cols method is just a function object in the local (class body) namespace - and it is called.
The error message you get is due to the missing self parameter, which is inserted only when you access your function as a method - but this error message is not what is wrong here: what is wrong is that you are making an imediate function call and expecting an special behavior, like late execution.
The way in Python to achieve what you want - i.e. to have the method called authomatically when the attribute colluns is accessed is to use the "property" built-in.
In this case, do just this: _columns = property(_get_dyna_cols) -
This will create a class attribute named "columns" which through a mechanism called "descriptor protocol" will call the desired method whenever the attribute is accessed from an instance.
To leran more about the property builtin, check the docs: http://docs.python.org/library/functions.html#property

Figure out child type with Django MTI or specify type as field?

I'm setting up a data model in django using multiple-table inheritance (MTI) like this:
class Metric(models.Model):
account = models.ForeignKey(Account)
date = models.DateField()
value = models.FloatField()
calculation_in_progress = models.BooleanField()
type = models.CharField( max_length=20, choices= METRIC_TYPES ) # Appropriate?
def calculate(self):
# default calculation...
class WebMetric(Metric):
url = models.URLField()
def calculate(self):
# web-specific calculation...
class TextMetric(Metric):
text = models.TextField()
def calculate(self):
# text-specific calculation...
My instinct is to put a 'type' field in the base class as shown here, so I can tell which sub-class any Metric object belongs to. It would be a bit of a hassle to keep this up to date all the time, but possible. But do I need to do this? Is there some way that django handles this automatically?
When I call Metric.objects.all() every objects returned is an instance of Metric never the subclasses. So if I call .calculate() I never get the sub-class's behavior.
I could write a function on the base class that tests to see if I can cast it to any of the sub-types like:
def determine_subtype(self):
try:
self.webmetric
return WebMetric
except WebMetric.DoesNotExist:
pass
# Repeat for every sub-class
but this seems like a bunch of repetitious code. And it's also not something that can be included in a SELECT filter -- only works in python-space.
What's the best way to handle this?
While it might offend some people's sensibilities, the only practical way to solve this problem is to put either a field or a method in the base class which says what kind of object each record really is. The problem with the method you describe is that it requires a separate database query for every type of subclass, for each object you're dealing with. This could get extremely slow when working with large querysets. A better way is to use a ForeignKey to the django Content Type class.
#Carl Meyer wrote a good solution here: How do I access the child classes of an object in django without knowing the name of the child class?
Single Table Inheritance could help alleviate this issue, depending on how it gets implemented. But for now Django does not support it: Single Table Inheritance in Django so it's not a helpful suggestion.
But do I need to do this?
Never. Never. Never.
Is there some way that django handles this automatically?
Yes. It's called "polymorphism".
You never need to know the subclass. Never.
"What about my WebMetric.url and my TextMetric.text attributes?"
What will you do with these attributes? Define a method function that does something. Implement different versions in WebMetric (that uses url) and TextMetric (that uses text).
That's proper polymorphism.
Please read this: http://docs.djangoproject.com/en/1.2/topics/db/models/#abstract-base-classes
Please make your superclass abstract.
Do NOT do this: http://docs.djangoproject.com/en/1.2/topics/db/models/#multi-table-inheritance
You want "single-table inheritance".

Django debug error

I have the following in my model:
class info(models.Model):
add = models.CharField(max_length=255)
name = models.CharField(max_length=255)
An in the views when i say
info_l = info.objects.filter(id=1)
logging.debug(info_l.name)
i get an error saying name doesnt exist at debug statement.
'QuerySet' object has no attribute 'name'
1.How can this be resolved.
2.Also how to query for only one field instead of selecting all like select name from info.
1. Selecting Single Items
It looks like you're trying to get a single object. Using filter will return a QuerySet object (as is happening in your code), which behaves more like a list (and, as you've noticed, lacks the name attribute).
You have two options here. First, you can just grab the first element:
info_l = info.objects.filter(id=1)[0]
You could also use the objects.get method instead, which will return a single object (and raise an exception if it doesn't exist):
info_l = info.objects.get(id=1)
Django has some pretty good documentation on QuerySets, and it may be worth taking a look at it:
Docs on using filters
QuerySet reference
2. Retrieving Specific Fields
Django provides the defer and only methods, which will let you choose specific fields from the database, rather than fetching everything at once. These don't actually prevent the fields from being read; rather, it loads them lazily. defer is an "opt-in" mode, which lets you specify what fields should be lazily loaded. only is "out-out" -- you call it, and only the fields you pass will by eagerly loaded.
So in your example, you'd want to do something like this:
info_l = info.objects.filter(id=1).only('name')[0]
Though with a model as simple as the example you give, I wouldn't worry much at all about limiting fields.

Categories

Resources