edoburu / django-any-urlfield Goto Github PK
View Code? Open in Web Editor NEWAn improved URL selector to choose between internal models and external URLs
License: Apache License 2.0
An improved URL selector to choose between internal models and external URLs
License: Apache License 2.0
Several clients on our sites have expressed the wish to put email addresses into AnyURL fields. The default URLRegistry could be extended to support these.
When I run syncdb on a fresh database it crashes complaining it can't find a relationship.
Looking at the stack trace it looks like its something to do with any url fields. If I remove my two calls to AnyUrlField.register_model()
in my Models.py it seems to fix it.
My hunch is that syncdb does some validation before running and this is where its trying to inspect the relationships created when registering a model with AnyURLField?
Here is my stack trace:
Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line utility.execute() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv self.execute(*args, **options.__dict__) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/management/base.py", line 284, in execute self.validate() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate num_errors = get_validation_errors(s, app) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors for (app_name, error) in get_app_errors().items(): File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors self._populate() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate self.load_app(app_name, True) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app models = import_module('%s.models' % app_name) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/debug_toolbar/models.py", line 63, in <module> patch_root_urlconf() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf reverse('djdt:render_panel') File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/urlresolvers.py", line 480, in reverse app_list = resolver.app_dict[ns] File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/urlresolvers.py", line 310, in app_dict self._populate() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/urlresolvers.py", line 262, in _populate for pattern in reversed(self.url_patterns): File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/urlresolvers.py", line 346, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/core/urlresolvers.py", line 341, in urlconf_module self._urlconf_module = import_module(self.urlconf_name) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name) File "/Users/matt/PROJECTS/client/2014/da2014/da/da/da/urls.py", line 9, in <module> admin.autodiscover() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 29, in autodiscover import_module('%s.admin' % app) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name) File "/Users/matt/PROJECTS/client/2014/da2014/da/da/core/admin.py", line 1, in <module> from .forms import (ChildPageForm, NewsPageForm, BlogPageForm, HomePageForm, File "/Users/matt/PROJECTS/client/2014/da2014/da/da/core/forms.py", line 22, in <module> class HomePagePromotionForm(ModelForm): File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/forms/models.py", line 282, in __new__ opts.help_texts, opts.error_messages) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/forms/models.py", line 201, in fields_for_model formfield = f.formfield(**kwargs) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/any_urlfield/models/fields.py", line 81, in formfield return super(AnyUrlField, self).formfield(**kwargs) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 705, in formfield return super(CharField, self).formfield(**defaults) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 554, in formfield return form_class(**defaults) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/any_urlfield/forms/fields.py", line 44, in __init__ widget = self.widget(url_type_registry=url_type_registry) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/any_urlfield/forms/widgets.py", line 71, in __init__ if getattr(form_field, 'choices', None) and getattr(widget, 'choices', None): File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/forms/models.py", line 1048, in __len__ return len(self.queryset) +\ File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__ self._fetch_all() File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all self._result_cache = list(self.iterator()) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator for row in compiler.results_iter(): File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter for rows in self.execute_sql(MULTI): File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 782, in execute_sql cursor.execute(sql, params) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute return self.cursor.execute(sql, params) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Users/matt/.virtualenvs/da2014/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: relation "core_hubpage" does not exist LINE 1: ...core_hubpage"."level", "core_hubpage"."body" FROM "core_hubp...
Registering the same model from different apps raises:
ValueError: Model is already registered: '<class 'MyModel'>'
but this should not happen, because AnyUrlField
could be used by different apps and each app should be allowed to register all the models that it needs, regardless of the models registered by other apps.
Possible solutions:
Don't raise ValueError
, just ignore the registration of a model already registered.
When registering a model, check from which app the model is registered and raise a ValueError
only if the model has been registered more than once from the same app.
It would be very useful to have the possibility to filter models, for example if you don't want that backend users set the url of an unpublished object.
It would be nice doing it by passing the manager, like in get_object_or_404
:
AnyUrlField.register_model(Products.objects_published, widget=SimpleRawIdWidget(Products))
Thank you for this great field.
Let's say I have a category
model with a urlfield that has urls for articles
. Do I register the article
class in articles/models.py
or in categories/models.py
?
I'm trying to set up object caching for my Django project but the Any Urlfields I'm using are keeping the python serializer from pickling. It seems that this use of a lambda is causing it to throw this error:
Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
Is there a reason that you want to leave this callable?
Could the lambda
be removed?
form_field = forms.ModelChoiceField(queryset=model._default_manager.all(), widget=widget)
Or if it needs to be callable, a partial function could work:
from functools import partial
def model_choice_field(model, widget):
return forms.ModelChoiceField(queryset=model._default_manager.all(), widget=widget)
....
form_field = partial(model_choice_field, model, widget)
...
/usr/lib/python2.7/site-packages/any_urlfield/forms/fields.py:10: RemovedInDjango19Warning: The django.forms.util module has been renamed. Use django.forms.utils instead.
from django.forms.util import ErrorList
...
It would be very useful to have the possibility to pass a field option like external_url=False
for disallowing linking to external pages.
...
/usr/lib/python2.7/site-packages/any_urlfield/models/values.py:7: RemovedInDjango19Warning: The utilities in django.db.models.loading are deprecated in favor of the new application loading system.
from django.db.models.loading import get_model
...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.