Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
correctly handle cases when primary key can not be determined
Browse files Browse the repository at this point in the history
  • Loading branch information
bubenkoff committed Jan 15, 2015
1 parent 4d6673e commit e93739f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
@@ -1,7 +1,7 @@
Changelog
=========

1.0.0
1.0.1
-----

* No logging of exception getting the id from the value (bubenkoff)
Expand Down
2 changes: 1 addition & 1 deletion forms2/__init__.py
@@ -1,7 +1,7 @@
"""Extends Django forms with the classes that support individual field access control and edit the SQLAlchemy models."""
__all__ = ('SAModelForm', 'FieldAccessForm', 'ModelChoiceField', 'FieldAccess', 'ModelMultipleChoiceField')

__version__ = '1.0.0'
__version__ = '1.0.1'

try:
from django import forms
Expand Down
23 changes: 14 additions & 9 deletions forms2/sqlalchemy.py
Expand Up @@ -18,8 +18,7 @@


def model_to_dict(instance, keys, mapping=None):
"""Returns a dict containing the data in ``instance`` suitable for passing as
a Form's ``initial`` keyword argument.
"""Return a dict containing the data in ``instance`` suitable for passing as a Form's ``initial`` keyword argument.
``keys`` is an optional list of field names. If provided, only the named
fields will be included in the returned dict.
Expand All @@ -43,7 +42,7 @@ def model_to_dict(instance, keys, mapping=None):


def dict_to_model(instance, data, mapping=None):
"""Assigns the values in the data dictonary to an instance.
"""Assign the values in the data dictonary to an instance.
``data`` is the cleaned data of the form.
Expand All @@ -62,6 +61,7 @@ def dict_to_model(instance, data, mapping=None):


class BaseModelForm(forms.Form):

"""Base class for the SQLAlchemy model form.
It implements initials based on the instance and save the instance, taking
Expand All @@ -70,8 +70,8 @@ class BaseModelForm(forms.Form):
By default, save() will call self.save_instance().
It is necessary to override save_instance() with your own implementation.
save_instance() is responsible for writing self.instance to the database.
"""

def __init__(self, instance=None, data=None, files=None, *args, **kwargs):
self._meta = self.Meta
self.instance = instance or self._meta.model()
Expand All @@ -98,6 +98,7 @@ def save_instance(self):


class ModelChoiceField(forms.ModelChoiceField):

"""SQLAlchemy compatible multiple choice field."""

def __init__(self, *args, **kwargs):
Expand All @@ -111,13 +112,15 @@ def label_from_instance(self, obj):

@property
def primary_key(self):
if sa_version >= '0.8':
from sqlalchemy import inspect
return inspect(self.queryset._entities[0].entities[0]).primary_key[0]
return self.queryset._entities[0].entity.primary_key[0]
if self.queryset is not None:
if sa_version >= '0.8':
from sqlalchemy import inspect
return inspect(self.queryset._entities[0].entities[0]).primary_key[0]
return self.queryset._entities[0].entity.primary_key[0]

def prepare_value(self, value):
return getattr(value, self.primary_key.name, value)
if self.primary_key is not None:
return getattr(value, self.primary_key.name, value)

def to_python(self, value):
if value in EMPTY_VALUES:
Expand All @@ -129,7 +132,9 @@ def to_python(self, value):


class ModelMultipleChoiceField(ModelChoiceField, forms.ModelMultipleChoiceField):

"""SQLAlchemy compatible multiple choice field."""

def clean(self, value):
if self.required and not value:
raise forms.ValidationError(self.error_messages['required'])
Expand Down

0 comments on commit e93739f

Please sign in to comment.