charles-vdulac / django-roa Goto Github PK
View Code? Open in Web Editor NEWFork of original Django ROA lib. Now ROA works directly with an API like Django Rest Framework
License: BSD 3-Clause "New" or "Revised" License
Fork of original Django ROA lib. Now ROA works directly with an API like Django Rest Framework
License: BSD 3-Clause "New" or "Revised" License
Hi, i'm trying to create a proxy for some ROA model that will use a normal manager insted of a ROAManager to make queries in a SQL database (i have read access only, that's why i use ROA). The problem is that i can't set the default manager correctly. This is part of my code:
class SomeROAModelProxy(SomeROAModel):
class Meta(SomaROAModel.Meta):
proxy = True
objects = models.Manager()
and here is my problem:
>>> SomeROAModel._default_manager
<django_roa.db.managers.ROAManager at 0x2470250>
>>> SomeROAModelProxy._default_manager
<django_roa.db.managers.ROAManager at 0x303d2d0>
>>> SomeROAModel._default_manager is SomeROAModelProxy._default_manager
False
The manager of SomeROAModelProxy shoud be a models.Manager instance. If I do the same with the User model for example, it works well:
class UserProxy(User):
class Meta(User.Meta):
proxy = True
objects = models.Manager()
>>> User._default_manager
<django.contrib.auth.models.UserManager at 0x33ded50>
>>> UserProxy._default_manager
<django.db.models.manager.Manager at 0x3fbaa10>
Is there an authentication mechanism for django-roa?
Docs on https://code.larlet.fr/django-roa/wiki/GettingStarted raise issues that Chrome and Safari won't let you view the docs.
In your DRF example you have frontend and backend apps.
In each, you are defining the models for "Articles" "Tags" etc...
But if for example there was a model in the front end, and you wanted to create a relationship to the "backend" between the two, how would you handle this?
I have followed the examples and have hit an error I can't seem to figure out. It seems that at this line of code the serializer
variable is None
. The line just before it it calls the method get_serializer()
on the model and I defined the serialize()
method in my model. Also for context in my view I am calling Market.objects.all()
which is where the following error occurs.
Error:
AttributeError at /markets/
'NoneType' object has no attribute 'is_valid'
Request Method: GET
Request URL: http://127.0.0.1:8000/markets/
Django Version: 1.6.4
Exception Type: AttributeError
Exception Value:
'NoneType' object has no attribute 'is_valid'
Exception Location: /Users/mdahl/Documents/Repositories/django-test/src/django-roa/django_roa/db/query.py in iterator, line 217
Python Executable: /Users/mdahl/Documents/Repositories/django-test/bin/python
Python Version: 2.7.6
Python Path:
['/Users/mdahl/Documents/Repositories/django-test/djangotest',
'/Users/mdahl/Documents/Repositories/django-test/src/django-roa',
'/Users/mdahl/Documents/Repositories/django-test/lib/python27.zip',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/plat-darwin',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/plat-mac',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/lib-tk',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/lib-old',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/lib-dynload',
'/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/site-packages']
Server time: Fri, 23 May 2014 23:52:23 +0000
Settings:
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_roa',
'main',
)
...
API_BASE_URL = 'http://127.0.0.1/inventory/api/v1/'
ROA_MODELS = True # set to False if you'd like to develop/test locally
ROA_FORMAT = 'json' # json or xml
ROA_DJANGO_ERRORS = True # useful to ease debugging if you use test server
Model:
from django.conf import settings
from django.db import models
from django_roa import Model as ROAModel
class ApiModel(ROAModel):
@classmethod
def get_resource_url_list(cls):
return u'{base_url}{base_name}/'.format(
base_name=cls.api_base_name,
base_url=settings.API_BASE_URL,
)
def get_resource_url_count(self):
return self.get_resource_url_list()
class Meta:
abstract = True
class Market(ApiModel):
api_base_name = 'market'
id = models.IntegerField(primary_key=True)
display_name = models.CharField(max_length=64)
notes = models.TextField(default='')
@classmethod
def serializer(cls):
from serializers import MarketSerializer
return MarketSerializer
Serializer:
from rest_framework import serializers
from models import Market
class MarketSerializer(serializers.ModelSerializer):
class Meta:
model = Market
fields = (
'id',
'display_name',
'notes',
)
The ROA_MODELS
setting seems to only be set on application load, preventing it from being toggled in a test case like in the following example. It seems it would be useful if it were designed so this setting could be toggled in this manner for non-integration tests.
class TestUser(unittest.TestCase):
def setUp(self):
# Use local database for models as these tests are not integration tests
settings.ROA_MODELS = False
def tearDown(self):
# Revert setting back to use API for database
settings.ROA_MODELS = True
def test_create(self):
user = User(name='John Doe')
try:
user.save()
self.assertTrue(True)
except Exception as e:
self.fail('Failed to create user with exception: %s' % e)
Looking at how the ROA_MODELS
setting is currently implemented I am not sure how to achieve testing locally without actually changing the setting in settings.py
, which would then break the functionality of running the dev server against an actual API server. It would be rather tedious to constantly change this setting back and forth depending on if I am running the dev server versus tests.
Can you pls put to the repo an example server project made with Django Rest Framework. I didn't manage to make your version of django-roa work properly with my own simple server on Django Rest Framework. Maybe it requires some kind of special configuration of server side which I'm not aware of.
Thanks.
Hello,
I was wondering if you could provide more detail on how to make the following calls. I searched on the github and wiki page and was unable to understand how to make the calls.
Thank you
Just putting it up here, a quick win would be adding 3.3 and 3.4 to the travis.ci test suites, haven't dug in deeper yet.
I tried to run django_roa_server from examples following the instructions on https://bitbucket.org/david/django-roa/wiki/GettingStarted but I get this error:
Traceback (most recent call last):
File "manage.py", line 4, in
from django.core.management import execute_manager
ImportError: cannot import name execute_manager
I already tried using a new manage.py file but it didn't run neither. Could you help me to run it?
Hii, I'm using ROA as a client of a Django Rest Framework API. I defined in both sides a basic many to many field. In DRF i can read and write this via json as a list ok primary keys.
In ROA i can read this field correctly but for writing I can't: The clear() method doesn't work and add() throws thid error:
Cannot add "<Model: XXXX>": instance is on database "None", value is on database "default"
It seems that this happens because it reads the model on the local database and not via the API.
Some useful information:
>>> model = Model.objects.first()
>>> model.m2m_field
<django.db.models.fields.related.ManyRelatedManager at 0x7f2323f6a810>
>>> model.m2m_field.is_roa_manager
True
How can I solve this?
Pasting IPython output
In [92]: # Normal queryset
In [93]: qs = User.objects.all()
In [94]: another_qs = qs.filter(pk=1)
In [95]: qs is another_qs
Out[95]: False
In [96]: another_qs # Only the filtered ones
Out[96]: [User: user1]
In [97]: qs # Should display all users
Out[97]: [User: user1, User: user2, User: user3, User: user4]
In [98]: # Queryset using ROA
In [99]: qs = SomeRoaModel.objects.all()
In [100]: another_qs = qs.filter(pk=1)
In [101]: qs is another_qs
Out[101]: False
In [102]: another_qs # Only the filtered ones
Out[102]: [SomeRoaModel: AAA]
In [103]: qs # Should display all objects (AAA, BBB, CCC and DDD)
Out[103]: [SomeRoaModel: AAA] <-- It shows only the another_qs' objects instead of the qs ones
This problem is breaking the validation of a ROAModel in my admin site, how can it be solved?
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.