Access to related Objects inside a model propery - django-models

I just run into some problems with django models.
Example code is better than any word:
class Cart(models.Model):
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return u'date %s;'%(self.created_at)
def __str__(self):
return self.__unicode__()
def _total_items(self):
""" Totale n di oggetti """
a = 0
for i in self.items.all:
a += i.quantity
return a
total_items = property(_total_items)
class Item(models.Model):
cart = models.ForeignKey(Cart)
quantity = models.PositiveIntegerField()
def __unicode__(self):
return u'product %s'%(self.id)
def __str__(self):
return self.__unicode__()
but, when i call the cart property here's what i get in the python console:
>>> a.total_items
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "models.py", line 49, in _total_items
for i in self.item_set.all:
TypeError: 'RelatedManager' object is not callable

Try replacing this line
for i in self.items.all:
with this one
for i in self.items.all():

Related

How to show the first x characthers of a Django model field in self representation

How can I use the first, let's say 10 characters of a field in the string representation of a Django model entry?
If I simply use {self.Post} I get the whole thing that might be too long. I tried to use {self.Post,10} but that doesn't really fly.
class Posts(models.Model):
Poster = models.ForeignKey(
User, on_delete=models.CASCADE, verbose_name="Poster")
PostCreated = models.DateTimeField(
auto_now_add=True, null=True, verbose_name="Post created")
Post = models.TextField(blank=True, verbose_name="Post")
PostEdited = models.BooleanField(
default=False, verbose_name="Has been edited")
PostHasComments = models.BooleanField(
default=False, verbose_name="Has comments")
def __str__(self):
return f"{self.Post} by {self.Poster}"
class Meta:
verbose_name_plural = "Posts"
You can slice the post, with:
def __str__(self):
return f'{self.Post[:10]} by {self.Poster}'

Pass arguments to aiohttp class based view

Is there any simple way to pass custom arguments to View instances with aiohttp ?
This works:
import aiohttp.web
import functools
class Parent():
def __init__(self, val):
self.var = val
class BaseView(aiohttp.web.View):
def __init__(self, *args, **kwargs):
self.parent = kwargs.pop("parent")
super().__init__(*args, **kwargs)
class Handler(BaseView):
async def get(self):
return aiohttp.web.Response(text=self.parent.var)
def partial_class(cls, *args, **kwargs):
class NewCls(cls):
__init__ = functools.partialmethod(cls.__init__, *args, **kwargs)
return NewCls
def main():
parent = Parent("blablabla")
app = aiohttp.web.Application()
# New method with args
app.router.add_view_with_args = functools.partial(
lambda this, path, handler, d: this.add_view(path, partial_class(handler, **d)),
app.router,
)
# Tornado-style
app.router.add_view_with_args("/test", Handler, {"parent": parent})
aiohttp.web.run_app(app)
main()
But I feel like this is overcomplicated.
With Tornado, you can pass additionnal data as a dict object when you instanciate your web Application.
Answering my own question:
It turns out that you are allowed to store global-like variables in an Application instance and then access it in the request handler. It is described in the docs: https://docs.aiohttp.org/en/latest/web_advanced.html#application-s-config

Converting value to json inside serve method. Wagtail

I realy tried to find answer to my question, but don't know what should I do. I found following question and they didn't help me. question1, question2, docs
I got different values with different functions that I used. Sometimes None value
sometimes TypeError: Object of type 'method' is not JSON serializable
and
AttributeError: 'str' object has no attribute 'status_code' and this
TypeError: 'method' object is not iterable
But i didn't still find solution to solve my problem.
Here is my Page model it has InlinePanel that takes some data from another class:
class ScreencastPage(Page):
content_panels = Page.content_panels + [
InlinePanel(
'groupstage_screencast_relationship', label="Choose Teams",
panels=None, max_num=2),
]
parent_page_types = ['home.HomePage']
def matches(self):
matches = [
n.match for n in self.groupstage_screencast_relationship.all()
]
return matches
def serve(self, request):
if request.is_ajax():
# TODO Convert self.mathes to JSON and return it
else:
return super(ScreencastPage, self).serve(request)
And here is the model that related to my ScreencastPage
#register_snippet
class GroupstageTournamentModel(ClusterableModel):
number = models.PositiveSmallIntegerField(
verbose_name="Match №:")
starts_at = models.DateTimeField()
# Team 1
team_1 = models.ForeignKey(
TeamRooster,
null=True, verbose_name='Erste Team',
on_delete=models.SET_NULL,
related_name="+",
)
team_1_dress = ColorField(blank=True, verbose_name='Dress')
team_1_first_halftime_score = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Resultat 1. HZ')
team_1_first_halftime_point = models.PositiveSmallIntegerField(blank=True, default=0, verbose_name='Punkte 1. HZ')
...
UPDATE
Sorry if I ask too noob questions, but I'm new in programming. #gasman these are the ways I used.
1
def serve(self, request):
if request.is_ajax():
lst = []
d = {}
for pn in self.matches:
d['mpn']=pn
lst.append(d)
return json.dumps([dict(mpn=pn) for pn in lst])
returns: TypeError: 'method' object is not iterable
2
Just changed loop from for pn in self.matches: to for pn in self.matches():
def serve(self, request):
if request.is_ajax():
lst = []
d = {}
for pn in self.matches():
d['mpn']=pn
lst.append(d)
return json.dumps([dict(mpn=pn) for pn in lst])
returns: TypeError: Object of type 'GroupstageTournamentModel' is not JSON serializable
3
def serve(self, request):
if request.is_ajax():
if isinstance(self.matches, (list, dict, str, int, float, bool, type(None))):
data = JSONEncoder.default(self.matches())
return data
elif '_python_object' in self.matches():
data = pickle.loads(str(self.matches['_python_object']))
return data
returns: ValueError: The view wagtail.wagtailcore.views.serve didn't return an HttpResponse object. It returned None instead.
4
def serve(self, request):
if request.is_ajax():
data = [
n.match for n in self.groupstage_screencast_relationship.all()
]
return data
returns: AttributeError: 'list' object has no attribute 'status_code'
5
def serve(self, request):
if request.is_ajax():
data = [
n.match for n in self.groupstage_screencast_relationship.all()
]
if isinstance(data, (list, dict, str, int, float, bool, type(None))):
conv_data = json.JSONEncoder.default(data)
return conv_data
returns: TypeError: default() missing 1 required positional argument: 'o'
As I said, I do not know how this conversion works, so I tried to guess.
The important lesson here is to try to solve one problem at once. You're trying to deal with returning a response from serve at the same time as constructing some JSON, and it doesn't look like you're getting anywhere because fixing the first half of the problem just leads you to an error in the second half.
Let's make sure we know how to return something from serve, even if it's just something useless:
def serve(self, request):
if request.is_ajax():
return "hello world!"
else:
return super(ScreencastPage, self).serve(request)
This will fail with something like: 'str' object has no attribute 'get'. This tells us that returning a string is the wrong thing to do: whatever object we return, Wagtail is expecting it to have a get attribute. Looking at the documentation, we can see that it's supposed to be an HttpResponse object:
from django.http import HttpResponse
def serve(self, request):
if request.is_ajax():
return HttpResponse("hello world!")
else:
return super(ScreencastPage, self).serve(request)
This works, so now we know that whatever other stuff we do with JSON in this method, we need to end with return HttpResponse(some_result).
So now let's bring in json.dumps. Again, let's start with some fake data to make sure we're using it right:
import json
from django.http import HttpResponse
def serve(self, request):
if request.is_ajax():
result = ['first match', 'second match']
json_output = json.dumps(result)
return HttpResponse(json_output)
else:
return super(ScreencastPage, self).serve(request)
Hopefully this works too, so let's bring in the real data:
import json
from django.http import HttpResponse
def serve(self, request):
if request.is_ajax():
result = self.matches()
json_output = json.dumps(result)
return HttpResponse(json_output)
else:
return super(ScreencastPage, self).serve(request)
This now fails with something like: TypeError: Object of type 'GroupstageTournamentModel' is not JSON serializable. So now you have to ask: what's changed here? What's different about my real data from the 'fake' data? If you're not sure, add in a debugging line to see what's going on:
import json
from django.http import HttpResponse
def serve(self, request):
if request.is_ajax():
result = self.matches()
print(result) # this output will appear in the terminal / command prompt
json_output = json.dumps(result)
return HttpResponse(json_output)
else:
return super(ScreencastPage, self).serve(request)
The error message hopefully makes it clear: the value you're passing to json.dumps contains GroupstageTournamentModel objects, and JSON doesn't know how to deal with those. You need to convert them into basic values such as dicts, and that means specifying how each individual field is meant to appear in the output:
def serve(self, request):
if request.is_ajax():
result = [
{
'number': match.number,
'team1': match.team_1.name,
# ...
}
for match in self.matches()
]
json_output = json.dumps(result)
return HttpResponse(json_output)
else:
return super(ScreencastPage, self).serve(request)
In summary - when you encounter an error message:
don't just abandon your code and try something else;
look at what the error message is telling you, and especially, what line of code it's coming from;
see if there's a way to reduce it to a simpler case that does succeed, then work your way back up to the real solution.

How to solve some sort of chicken egg relation within ndb.Models?

I have two entities (events and users). Each user has several events, but I don't want them to be stored within a StructuredProperty, because in future it should be possible to have multiple creators/admins. Now I have the problem, that User needs the Event Class for definition and vice versa. How can I implement the intended structure?
Two models with mutual relations.
class Event(EndpointsModel):
_message_fields_schema = ("id", "name", "creator",
"datetime", "place", "category", "participants")
creator = ndb.KeyProperty(kind=User)
participants = ndb.KeyProperty(kind=User, repeated=True)
name = ndb.StringProperty()
datetime = ndb.DateTimeProperty(auto_now_add=True)
place = ndb.GeoPtProperty()
category = ndb.StringProperty(choices=('all', 'drinking'))
class User(EndpointsModel):
_message_fields_schema = ("id", "name", "password", "events")
name = ndb.StringProperty()
password = ndb.StringProperty()
events = ndb.KeyProperty(kind=Event, repeated=True)
def create_event(self, e_name, e_datetime, e_place, e_category):
event = Event(name=e_name, creator = self.key, datetime=e_datetime, place=e_place, category=e_category)
event.put()
self.events.append(event)
self.put()
def get_events(self):
return ndb.get_multi(self.events)
Error Message:
NameError: name 'User' is not defined
EDIT 1:
I changed the kind to a string, containing the class name, like Greg suggested it. But it does not work too.
class Category(EndpointsModel):
_message_fields_schema = ("id", "name", "parent")
name = ndb.StringProperty()
parent = ndb.KeyProperty(kind='Category', default=None)
class Event(EndpointsModel):
_message_fields_schema = ("id", "name", "creator", "datetime",
"place", "category")
participants = ndb.KeyProperty(kind='User', repeated=True)
creator = ndb.KeyProperty(kind='User')
name = ndb.StringProperty()
datetime = ndb.DateTimeProperty(auto_now_add=True)
place = ndb.GeoPtProperty()
category = ndb.KeyProperty(Category)
class User(EndpointsModel):
_message_fields_schema = ("id", "name", "password")
name = ndb.StringProperty()
password = ndb.StringProperty()
events = ndb.KeyProperty(Event, repeated=True)
Now I receive the following stack trace:
ERROR 2014-01-21 09:38:39,764 service.py:191] Encountered unexpected error from ProtoRPC method implementation: BadValueError (Expected Key, got [])
Traceback (most recent call last):
File "/home/chris/Downloads/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
response = method(instance, request)
File "/home/chris/Downloads/google_appengine/lib/endpoints-1.0/endpoints/api_config.py", line 1321, in invoke_remote
return remote_method(service_instance, request)
[...]
value = self._call_shallow_validation(value)
File "/home/chris/Downloads/google_appengine/google/appengine/ext/ndb/model.py", line 1227, in _call_shallow_validation
return call(value)
File "/home/chris/Downloads/google_appengine/google/appengine/ext/ndb/model.py", line 1274, in call
newvalue = method(self, value)
File "/home/chris/Downloads/google_appengine/google/appengine/ext/ndb/model.py", line 1927, in _validate
raise datastore_errors.BadValueError('Expected Key, got %r' % (value,))
BadValueError: Expected Key, got []
You can use strings in the KeyProperty constructor to refer to kinds that don't have a model definition:
class Event(ndb.Model):
participants = ndb.KeyProperty(kind='User', repeated=True)
You can not create such references to the entities. Here is somo solutions:
1. You must use normal StringProperty for Event.creator or other id for User instance
2. Remove evens from class User - you can reach to evens by index on class Events
3. Use third entity model like this:
class EventCreator(EndpointsModel):
creator = ndb.KeyProperty(kind=User)
event = ndb.KeyProperty(kind=Event)
and from class User remove creator & from class Event remove
You could specify the key properties without the kind parameter (it is optional) and then do a manual check in your constructor or a pre-put hook or something like that -- or maybe not even worry about the kind:
class Event(EndpointsModel):
creator = ndb.KeyProperty()
# Constructor option
def __init__(self, *args, **kwargs):
super(Event, self).__init__(*args, **kwargs)
if 'creator' in kwargs and kwargs['creator'] != 'User':
raise Exception('oh no')
# Hook option
_pre_put_hook(self):
if self.creator and self.creator.kind() != 'User':
raise Exception("oh no")
The actual syntax will probably be slightly different. Feel free to edit.

ndb get & get_or_insert how to use ? (alway raise Exception)

I write code as below
from google.appengine.ext import ndb
__metaclass__ = type
class UserSession(ndb.Model):
session = ndb.BlobProperty()
class KV:
#staticmethod
def get(id):
r = ndb.Key(UserSession, int(id)).get()
if r:
return r.session
#staticmethod
def set(id, value):
return UserSession.get_or_insert(int(id), session=value)
#staticmethod
def delete(id):
ndb.Key(UserSession, int(id)).delete()
where I write
id = 1
key = ndb.Key(UserSession, int(id))
UserSession.get_or_insert(key, session=1)
the sdk raise
TypeError: name must be a string; received Key('UserSession', 1)
when I call KV.get ()
the sdk raise
File "/home/bitcoin/42btc/zapp/_plugin/auth/model/gae/user.py", line 14, in get
r = ndb.Key(UserSession,int(id)).get()
...
BadRequestError: missing key id/name
So , how to use NDB?
The get_or_insert() method takes a string which is only the ID part of the key, not a Key. It cannot use numeric IDs.

Resources