Counting mongoengine ListField elements - django

I`m having a hard time migrating my models from standard Django orm with MySQL to mongoengine-odm.
I have the following model that works fine in the old structure:
class Place(Document):
name = StringField()
acronym = StringField()
parent = ReferenceField('self')
hierarchy = ListField(ReferenceField('self'))
hierarchy_size = IntField()
#classmethod
def preSave(instance, sender, **kwargs):
instance.hierarchy_size = len(instance.hierarchy)
signals.pre_save.connect(Place.preSave, sender=Place)
But when working with the new mongoengine, I'm facing problems to access the objects hierarchy size in the way I was doing before. Receiving:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/awesome_user/workspace/awesome_project/project/models.py", line 728, in importPlacesFromMySQL
myCountry.save()
File "/Users/awesome_user/workspace/awesome_project/site_env/lib/python2.7/site-packages/mongoengine/document.py", line 220, in save
signals.pre_save.send(self.__class__, document=self)
File "/Users/awesome_user/workspace/awesome_project/site_env/lib/python2.7/site-packages/blinker/base.py", line 267, in send
for receiver in self.receivers_for(sender)]
File "/Users/awesome_user/workspace/awesome_project/project/models.py", line 804, in preSave
instance.hierarchy_size = len(instance.hierarchy)
TypeError: object of type 'ListField' has no len()
Does anyone know how to access the properties of the instance, like the length in my case?

You have a mistake in defining preSave classmethod. You are trying to take a len from hierarchy ListField from Place class (not instance). This is because you are using the first argument of the method - it's not an instance, it's a class itself. Use the 3rd argument instead:
#classmethod
def preSave(cls, sender, document, **kwargs):
document.hierarchy_size = len(document.hierarchy)

len(object.field) will retrieve all child documents and then count. If you only want the length of the array without retrieving all the documents you can use len(object.to_mongo()["field"])

Related

Get Bounding Box from Django PolygonField

I am trying to get the bounding box for a PolygonField in a Django model. I read here that you can use extent but when running the below as per the extent documentation:
qs = newJob.objects.filter(pk=1).aggregate(Extent('loc'))
print(qs['loc__extent'])
I get the error:
Traceback (most recent call last): File
"/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py",
line 86, in _execute
return self.cursor.execute(sql, params) psycopg2.errors.UndefinedFunction: function st_extent(geography) does
not exist LINE 1: SELECT ST_Extent("app_newjob"."loc") AS
"loc__ext...
^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
My model (simplified) looks like this:
from django.contrib.gis.db import models
class newJob(models.Model):
name = models.CharField(max_length=64)
desc = models.CharField(max_length=64)
loc = models.PolygonField()
And in my test:
from django.contrib.gis.db.models import Extent
newJob.objects.create(
name="test",
desc="test",
loc="SRID=4326;POLYGON ((0.9063720703125029 52.32023207609735, 0.8239746093749998 52.10819209746323, 1.170043945312496 52.14191683166823, 1.170043945312496 52.31351619974807, 0.9063720703125029 52.32023207609735))"
)
qs = newJob.objects.filter(pk=1).aggregate(Extent('loc'))
print(qs['loc__extent'])
I'm running a PostGIS database on the back.
Any ideas what I'm doing wrong?

Django Object is Not Serializable CommandError using Dumpdata with Natural Keys

I am trying to use 'natural keys' for serialization (docs) in a "manage.py dumpdata" command:
python manage.py dumpdata --natural-primary --natural-foreign --indent 4 --format json --verbosity 1 > tests\test_fixtures\test_db2.json
and I am getting the following error when I use --natural-foreign on other apps that use the Project or Task model (which they all must by design):
CommandError: Unable to serialize database: Object of type Project is not JSON serializable
Exception ignored in: <generator object cursor_iter at 0x000001EF62481B48>
Traceback (most recent call last):
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\site-packages\django\db\models\sql\compiler.py", line 1586, in cursor_iter
cursor.close()
sqlite3.ProgrammingError: Cannot operate on a closed database.
If I just dumpdata from this, the 'projects' app, it works, but other apps are built with entities related to Project or Task and there the --natural-foreign option fails.
The problem occurs when a model (say Question) calls for a natural_key from Task, which includes a for a natural_key from Project.
If I use the Pycharm Python Console to access querysets of Projects or Tasks ('q' here), this works:
serializers.serialize('json', q, indent=2, use_natural_foreign_keys=True, use_natural_primary_keys=True)
But if 'w' is a list of Question objects from another app that have a Task foreign key I get this error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\site-packages\django\core\serializers\__init__.py", line 128, in serialize
s.serialize(queryset, **options)
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\site-packages\django\core\serializers\base.py", line 115, in serialize
self.end_object(obj)
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\site-packages\django\core\serializers\json.py", line 53, in end_object
json.dump(self.get_dump_object(obj), self.stream, **self.json_kwargs)
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\__init__.py", line 179, in dump
for chunk in iterable:
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\encoder.py", line 405, in _iterencode_dict
yield from chunks
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\encoder.py", line 405, in _iterencode_dict
yield from chunks
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\encoder.py", line 325, in _iterencode_list
yield from chunks
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\encoder.py", line 438, in _iterencode
o = _default(o)
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\site-packages\django\core\serializers\json.py", line 104, in default
return super().default(o)
File "C:\Users\Andrew\anaconda3\envs\Acejet_development\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Project is not JSON serializable
The models are:
# projects.models.py
class BaseModelWithHistory(models.Model):
history = HistoricalRecords(inherit=True)
natural_key_fields = ('id',) # default
class Meta:
abstract = True
def natural_key(self):
fieldlist = [getattr(self, fieldname) for fieldname in self.natural_key_fields]
return tuple(fieldlist)
# natural_key.dependencies = ['projects.Project', 'projects.Task'] # serialize these first.
class Project(BaseModelWithHistory):
"""
'Projects' group Tasks.
"""
project_name = models.CharField(max_length=200, default="development_project")
project_short_description = models.CharField(
max_length=500,
default="This is the default text.")
target_group = models.ManyToManyField(Group, blank=True)
objects = ProjectDiscreteManager()
natural_key_fields = ('project_name',)
class Task(BaseModelWithHistory):
number = models.PositiveIntegerField(default=0)
name = models.CharField(max_length=200, default='new task')
project = models.ForeignKey(Project, on_delete=models.CASCADE)
target_group = models.ManyToManyField(Group, blank=True)
app_label = models.CharField(max_length=80, choices=app_choices(), null=True, blank=True)
objects = discrete_manager_factory('project')
natural_key_fields = ('project', 'name', 'number')
Things that I wouldn't expect to cause this problem but I could certainly be wrong:
The ProjectDiscreteManager and others created by discrete_manager_factory() behave exactly as the default manager (models.Manager()), unless its called with a request from an identified user, in which case it adds a filter to see if that user is in the Group.
All models define the natural_keys tuple because the parent class defines it as ('id',); most models overwrite this with more representative fields.
With the natural_key.dependencies list set for all models to prioritize Project and then Task, I get a 'can't resolve dependencies' error for every other model. I think this ticket relates, but am not sure how to track down whether this fix is already in the Django 3.0.6 I'm using and I should just straighten up & fly right, or if my Han Solo 'this should wooork' will one day soon be rewarded. [Update: I worked out it is coming in Django 3.1.1, but I'm not sure that it is going to fix the "can't resolve dependencies" error I've created for myself.]

Django: What is the correct way to query by foreign key field's id?

I have two models:
class Organization(models.Model):
title = models.CharField(max_length=100)
class Folder(models.Model):
organization = models.ForeignKey("Organization",related_name='folders')
title = models.CharField(max_length=50)
Now I want to filter the folder by organization id. so I tried:
Folder.objects.filter(organization= 1)
Folder.objects.filter(organization_id= 1)
Folder.objects.filter(organization__id= 1)
Folder.objects.filter(organization__pk= 1)
Folder.objects.filter(organization= Organization.objects.get(id=1))
Believe it or not everything returns the same.
So anybody know what is the correct way to query by foreign key field's id?
update
but when try to create folder by:
Folder.objects.create(organization__id=1,title='hello')
got error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/query.py", line 370, in create
obj = self.model(**kwargs)
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/base.py", line 452, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'organization__id' is an invalid keyword argument for this function
but Folder.objects.create(organization_id=1,title='hello') works fine.
Django docs say that you should use Folder.objects.filter(organization__pk=1) in most cases.
Answer to the update:
Probably Folder.objects.create(organization_id=1,title='hello') works because Django appends "_id" to the field name to create its database column name.

Django: debug AttributeError: can't set attribute

NOTE: I finally found the bug, so the below text was valuable perhaps only to me. The short answer: I decided to create a model field out of an attribute I had earlier defined as a #property-method. The only place where I hadn't removed the #property-method was in the Orchid model.
After some tweaked and poking to my code, I suddenly get this error: AttributeError: can't set attribute. I haven't changed any of the code for Orchid, but I now get this error:
>>> orc = Orchid.objects.get(id=1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 301, in get
num = len(clone)
File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__
self._fetch_all()
File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
self._result_cache = list(self.iterator())
File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 230, in iterator
obj = model(*row_data)
File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/base.py", line 347, in __init__
setattr(self, field.attname, val)
AttributeError: can't set attribute
The definition for Orchid is class Orchid(FinancialReturnMixin, PeerPerformance). I haven't changed the FinancialReturnMixin, whose code is:
class FinancialReturnMixin(models.Model):
exclude_special_dividend = True
round_to = 4
shares_outstanding = models.FloatField(blank=True, null=True)
stock_price = models.FloatField(
verbose_name='quarter-end stock price',
blank=True, null=True)
class Meta:
abstract = True
app_label = 'snippets'
The second part of the Orchid class definition is PeerPerformance from which I have commented out the one change I had made. The definition for PeerPerformance is class PeerPerformance(DividendBookValueMixin) and all I did here was add 1 additional field to the model. DividendBookValueMixin is an abstract model.
I deleted my Orchid migrations, datatable and relevant south_migrationhistory entries. With class Orchid(models.Model), the Orchid model sets up fine. With class Orchid(PeerPerformance) the Orchid error remains. All my tests against PeerPerformance run. I can read and save PeerPerformance objects just fine.
>>> from peer.models import PeerPerformance as PP
>>> pp1 = PP.objects.get(id=1)
>>> pp1.dividend = 0.135
>>> pp1.save()
DividendBookValueMixin is the parent class for PeerPerformance. With class Orchid(DividendBookValueMixin) the error remains. All my tests agains DividendBookValueMixin run.
Any thoughts where to look?
(On linux)Sometime this occurs when you don't have read write permissions to the project directory or in virtualenv directory. Make sure you have apropriate read write permissions to the directory.

Django get method is yielding an error

I'm following a Django book (Django 1.0 Web Site Development). I'm finding that the book, although straight forward and easy to read, leaves out small details. However, this error that I'm getting, I have not been able to find a solution online. Thanks for any help.
Below, I added the Tag class to my models.py file.
from django.db import models
from django.contrib.auth.models import User
class Link(models.Model):
url = models.URLField(unique=True)
class Bookmark(models.Model):
title = models.CharField(max_length=200)
user = models.ForeignKey(User)
link = models.ForeignKey(Link)
class Tag(models.Model):
name = models.CharField(max_length=64, unique=True)
bookmarks = models.ManyToManyField(Bookmark)
Then I attempt to run the following code at the python shell:
from bookmarks.models.import *
bookmark = Bookmark.objects.get(id=1)
As a result, I get the following error:
Traceback (most recent call last):
File "(console)", line 1, in (module)
File "c:\Python27\lib\site\-packages\django\db\models\manager.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "c:\Python27\lib\site-packages\django\db\models\query.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: Bookmark matching query does not exist.
The error means just what it says. DoesNotExist is raised by QuerySet.get() if there is no object in the database that would match the conditions given to the QuerySet. In this case it means there is no Bookmark object in the database with an ID equal to 1.
Did you add any data in the Bookmark table yet? DoesNotExist is raised by get if there is no record corresponding to your query. i.e. if there is no record corresponding to id=1.