django passing arguments based on value - python

I have defined optional variables in my django model. In my view, I might have those values or they might be None. I want to create that object without worrying about sending a None argument to the django model.
For example, Book object has a title, but publisher is optional.
right now in my view I'm doing something like
if publisher is None:
Book.objects.create(title=title)
else:
Book.objects.create(title=title, publisher=publisher)
Now this isn't manageable if there are multiple optional fields. What's the solution?

How about using ** operator:
attrs = {'title': title}
if publisher is not None:
attrs['publisher'] = publisher
Book.objects.create(**attrs)
UPDATE alternative - using Model.save:
book = Book(title='title')
if publisher is not None:
book.publisher = publisher
book.save()

Take a look at this:
Call a function with argument list in python
Basically create an array like args and then pass it in as *args to the method required.
You can also do something similar with **kwargs. Take a look at this:
Passing a list of kwargs?

Branching off the other answers... try this
def make_book(**kwargs):
query = {key: value for key, value in kwargs.items() if value}
Book.objects.create(**query)
I suggest declaring this as a method on your models manager so that you can quickly make instances where ever you need them like
Book.objects.make_book(title="The Title",publisher=None)

Related

django pass field-name as variable in get_or_create

I am trying to see if I can pass field name as a variable in get_or_create (since I have a function where the key in the kwargs can vary)
Like so:
def convert_value(cell_value, field_to_lookup):
rem_obj, created = Rem.objects.get_or_create(field_to_lookup=cell_value)
print ('created? ',created)
return rem_obj
The above wont work since it would look for 'field_to_lookup' as the key.
This post suggests using getattr but not sure if that'll be applicable in this case since I will again need to assign the output to a variable
This post helped. Now passing the field-value pair as dict which allows passing variables for field names. Here's the code:
def convert_value(cell_value, field_to_lookup):
rem_obj, created = Rem.objects.get_or_create(**{field_to_lookup:cell_value})
print ('created? ',created)
return rem_obj
Alternatively, I could directly just pass the dict to the function.

flask sqlalchemy query with keyword as variable

Let's say I have a model like this:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
hometown = db.Column(db.String(140))
university = db.Column(db.String(140))
To get a list of users from New York, this is my query:
User.query.filter_by(hometown='New York').all()
To get a list of users who go to USC, this is my query:
User.query.filter_by(university='USC').all()
And to get a list of users from New York, and who go to USC, this is my query:
User.query.filter_by(hometown='New York').filter_by(university='USC').all()
Now, I would like to dynamically generate these queries based on the value of a variable.
For example, my variable might look like this:
{'hometown': 'New York'}
Or like this:
{'university': 'USC'}
... Or even like this:
[{'hometown': 'New York'}, {'university': 'USC'}]
Can you help me out with writing a function which takes a dictionary (or list of dictionaries) as an input, and then dynamically builds the correct sqlalchemy query?
If I try to use a variable for the keyword, I get this err:
key = 'university'
User.query.filter_by(key='USC').all()
InvalidRequestError: Entity '<class 'User'>' has no property 'key'
Secondly, I am not sure how to chain multiple filter_by expressions together dynamically.
I can explicitly, call out a filter_by expression, but how do I chain several together based on a variable?
Hope this makes more sense.
Thanks!
SQLAlchemy's filter_by takes keyword arguments:
filter_by(**kwargs)
In other words, the function will allow you to give it any keyword parameter. This is why you can use any keyword that you want in your code: SQLAlchemy basically sees the arguments a dictionary of values. See the Python tutorial for more information on keyword arguments.
So that allows the developers of SQLAlchemy to receive an arbitrary bunch of keyword arguments in a dictionary form. But you're asking for the opposite: can you pass an arbitrary bunch of keyword arguments to a function?
It turns out that in Python you can, using a feature called unpacking. Simply create the dictionary of arguments and pass it to the function preceded by **, like so:
kwargs = {'hometown': 'New York', 'university' : 'USC'}
User.query.filter_by(**kwargs)
# This above line is equivalent to saying...
User.query.filter_by(hometown='New York', university='USC')
filter_by(**request.args) doesn't work well if you have non-model query parameters, like page for pagination, otherwise you get errors like these:
InvalidRequestError: Entity '<class 'flask_sqlalchemy.MyModelSerializable'>' has no property 'page'
I use something like this which ignores query parameters not in the model:
builder = MyModel.query
for key in request.args:
if hasattr(MyModel, key):
vals = request.args.getlist(key) # one or many
builder = builder.filter(getattr(MyModel, key).in_(vals))
if not 'page' in request.args:
resources = builder.all()
else:
resources = builder.paginate(
int(request.args['page'])).items
Considering a model with a column called valid, something like this will work:
curl -XGET "http://0.0.0.0/mymodel_endpoint?page=1&valid=2&invalid=whatever&valid=1"
invalid will be ignored, and page is available for pagination and best of all, the following SQL will be generated: WHERE mymodel.valid in (1,2)
(get the above snippet for free if you use this boilerplate-saving module)
As pointed out by #opyate that filter_by(**request.args) doesn't work well if you have non-model query parameters, like page for pagination, the following alternative can be used too:
Assuming that page is being taken in the form of request.args.get(), then:
def get_list(**filters):
page = None
if 'page' in filters:
page = filters.pop('limit')
items = Price.query.filter_by(**filters)
if page is not None:
items = items.paginate(per_page=int(page)).items
else:
items = items.all()
return {
"items": items
}
and then the get function
def get(self):
hometown = request.args.get('hometown')
university = request.args.get('university')
page = request.args.get('page')
return get_list(**request.args)
I have tried implementing this on my flask application, and it works smoothly.
Of course, one drawback that can be is if there are multiple values like page that are not a part of the model, then each of them has to be defined separately in the get_list, but that can be done by list comprehension

ndb to_dict method does not include object's key

I am leveraging ndb's to_dict method to convert an object's properties into a python dict. From everything I can tell, this method does not include the object's key or parent within the dict as per the documentation:
https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_to_dict
However for my situation I need the key to be in the dict. My preference would be to leverage the builtin method and subclass it or something similar rather than create my own to_dict method.
What is the best way to accomplish this or am I missing something obvious? Thanks in advance.
FYI: I am not leveraging django for this project but instead straight python deployed up to gae.
You're not missing anything ;-)
Just add the key to the dictionary after you call to_dict, and yes override the method.
If you have multiple models that don't share the same base class with your custom to_dict, I would implement it as a mixin.
to define to_dict as a method of a Mixin class. you would
class ModelUtils(object):
def to_dict(self):
result = super(ModelUtils,self).to_dict()
result['key'] = self.key.id() #get the key as a string
return result
Then to use it.
class MyModel(ModelUtils,ndb.Model):
# some properties etc...
Another easy way to achieve that (without having to override to_dict) is to add a ComputedProperty that returns the id, like so:
class MyModel(ndb.Model):
# this property always returns the value of self.key.id()
uid = ndb.ComputedProperty(lambda self: self.key.id(), indexed=False)
# other properties ...
A ComputedProperty will be added to the result of to_dict just like any other property.
There are just two constraints:
Apparently the name of the property can not be key (since that would conflict with the actual key) and id doesn't work either.
This won't work if you don't specify a key or id when you create the object.
Also, the computed value will be written to the data store when you call put(), so it consumes some of your storage space.
An advantage is that this supports the include and exclude arguments of to_dict() out of the box.

Get field value within Flask-MongoAlchemy Document

I've looked at documentation, and have searched Google extensively, and haven't found a solution to my problem.
This is my readRSS function (note that 'get' is a method of Kenneth Reitz's requests module):
def readRSS(name, loc):
linkList = []
linkTitles = list(ElementTree.fromstring(get(loc).content).iter('title'))
linkLocs = list(ElementTree.fromstring(get(loc).content).iter('link'))
for title, loc in zip(linkTitles, linkLocs):
linkList.append((title.text, loc.text))
return {name: linkList}
This is one of my MongoAlchemy classes:
class Feed(db.Document):
feedname = db.StringField(max_length=80)
location = db.StringField(max_length=240)
lastupdated = datetime.utcnow()
def __dict__(self):
return readRSS(self.feedname, self.location)
As you can see, I had to call the readRSS function within a function of the class, so I could pass self, because it's dependent on the fields feedname and location.
I want to know if there's a different way of doing this, so I can save the readRSS return value to a field in the Feed document. I've tried assigning the readRSS function's return value to a variable within the function __dict__ -- that didn't work either.
I have the functionality working in my app, but I want to save the results to the Document to lessen the load on the server (the one I am getting my RSS feed from).
Is there a way of doing what I intend to do or am I going about this all wrong?
I found out the answer. I needed to make use of a computed_field decorator, where the first argument was the structure of my return value and deps was a set which contained the fields that this field was dependent on. I then passed the dependent fields into a function's arguments and there you have it.
#fields.computed_field(db.KVField(db.StringField(), db.ListField(db.TupleField(db.StringField()))), deps=[feedname, location])
def getFeedContent(a=[feedname, location]):
return readRSS(a['feedname'], a['location'])
Thanks anyway, everyone.

Hide filter items that produce zero results in django-filter

I have an issue with the django-filter application: how to hide the items that will produce zero results. I think that there is a simple method to do this, but idk how.
I'm using the LinkWidget on a ModelChoiceFilter, like this:
provider = django_filters.ModelChoiceFilter(queryset=Provider.objects.all(),
widget=django_filters.widgets.LinkWidget)
What I need to do is filter the queryset and select only the Provider that will produce at least one result, and exclude the others.
There is a way to do that?
Basically, you need to apply filters, and then apply them again, but on newly-generated queryset. Something like this:
f = SomeFilter(request.GET)
f = SomeFilter(request.GET, queryset=f.qs)
Now when you have correct queryset, you can override providers dynamically in init:
def __init__(self, **kw):
super(SomeFilter, self).__init__(**kw)
self.filters['provider'].extra['queryset'] = Provider.objects.filter(foo__in=self.queryset)
Not pretty but it works. You should probably encapsulate those two calls into more-efficient method on filter.
Maybe the queryset can be a callable instead of a 'real' queryset object. This way, it can be generated dynamically. At least this works in Django Models for references to other models.
The callable can be a class method in you Model.
If I understand your question correctly I believe you want to use the AllValuesFilter.
import django_tables
provider = django_filters.AllValuesFilter(
widget=django_filters.widgets.LinkWidget)
More information is available here: http://github.com/alex/django-filter/blob/master/docs/ref/filters.txt#L77

Categories