-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update for allowing to pass lookup_field=None #211
base: master
Are you sure you want to change the base?
Update for allowing to pass lookup_field=None #211
Conversation
In case of lookup_field=None passed to nested Fields, url will be created only with help of parent_lookup_kwargs. Should make OneToOne relation's hyper-links auto-creation easier.
That sounds very nice. Yet I cannot picture how it work. Could you add some test illustrating the new usage? A change in the Readme would also be helpful. |
As for the code. getattr(): attribute name must be string because of no condition or exception catching before calling lookup_value = getattr(obj, self.lookup_field) After adding I'll try to show it with example from my project. models: class Modules(models.Model):
slug = models.SlugField(max_length=10)
class Classes(models.Model):
module = models.ForeignKey(Modules, on_delete=models.CASCADE, related_name='classes')
slug = models.SlugField(max_length=10)
class Orders(models.Model):
classes = models.OneToOneField(Classes, on_delete=models.CASCADE, related_name='order', primary_key=True)
code = models.SlugField(max_length=10)
class Plans(models.Model):
order = models.ForeignKey(Orders, on_delete=models.CASCADE, related_name='plans')
slug = models.SlugField(max_length=10) related url patterns: API/ ^modules/$ [name='modules-list']
API/ ^modules/(?P<slug>[^/.]+)/$ [name='modules-detail']
API/ ^modules/(?P<module_slug>[^/.]+)/classes/$ [name='classes-list']
API/ ^modules/(?P<module_slug>[^/.]+)/classes/(?P<slug>[^/.]+)/$ [name='classes-detail']
API/ ^modules/(?P<module_slug>[^/.]+)/classes/(?P<class_slug>[^/.]+)/order/$ [name='class-order-detail']
API/ ^modules/(?P<module_slug>[^/.]+)/classes/(?P<class_slug>[^/.]+)/order/plans/$ [name='class-order-plans-list']
API/ ^modules/(?P<module_slug>[^/.]+)/classes/(?P<class_slug>[^/.]+)/order/plans/(?P<slug>[^/.]+)/$ [name='class-order-plans-detail'] Serializers. Here you can see how some hyper-link configurations work. class ModuleSerializer(ModelSerializer):
class Meta:
model = Modules
fields = '__all__'
class ClassSerializer(NestedHyperlinkedModelSerializer):
class Meta:
model = Classes
fields = '__all__'
extra_kwargs = {'url': {'lookup_field': 'slug'}}
parent_lookup_kwargs = {'module_slug': 'module__slug'}
# first Nested Field with lookup_field=None.
# It allows creation of parent's link to its OneToOne relation element's view
order_url = NestedHyperlinkedIdentityField(
view_name='class-order-detail',
lookup_field=None,
parent_lookup_kwargs={ 'module_slug': 'module__slug', 'class_slug': 'slug' }
)
class OrderSerializer(NestedHyperlinkedModelSerializer):
class Meta:
model = Orders
fields = '__all__'
parent_lookup_kwargs = { 'module_slug': 'classes__module__slug', 'class_slug': 'classes__slug' }
# Here is the same link as the ClassesSerializer's order_url. Only parent kwargs differs.
url = NestedHyperlinkedIdentityField(
view_name='class-order-detail',
lookup_field=None,
parent_lookup_kwargs=parent_lookup_kwargs
)
# Another link with lookup_field=None. This time for list of children: plans.
# This list needs only parent's parent kwargs, same as above url link
plans_url = NestedHyperlinkedIdentityField(
view_name='class-order-plans-list',
lookup_field=None,
parent_lookup_kwargs=parent_lookup_kwargs
)
class PlansSerializer(NestedHyperlinkedModelSerializer):
class Meta:
model = Plans My project differs a bit from this example, but let's say mechanis is same and it works with overriden |
It is first time I contribute to someone's else project so let me know what exatly that pull req should look like if there is something missing. |
Hi, @gretkierewicz. Sorry to letting you waiting so long, specially on your 1st contribution. What I guess people would expect from a pull-request is to:
👍: Your PR for sure fulfill the "not break existing stuff", as the tests are passing. Be said that I do not use this library every day, so is hard for me to imagine the code before/after merging the PR. If I need to use it today, I do not know "how to use this new feature" compared to before. And the descriptive code you provided, as good as it could be, is not being tested on todays or tomorrows versions. So, what is still missing here is an automated test. The tests folder have a lot of such. I recommend you to copy-past one and adapt to your case. What this adds of value to the PR?
For someone's first contribution, "please add tests" can be seem as too much burden. I invite you to see it in an opposite way: Having tests for your feature on a community project means that, for now on, this community will care to not break your private app in the future versions. This will be "saving" a lot of private maintenance time. Not doing automated tests means that every new version of this lib will force you to test yourself and fix every break, as nobody else is seeing your stuff breaking. Again sorry for the late and lengthy answer. Hope to be merging your work soon, after got tests added. Best regards. |
In case of lookup_field=None passed to nested Fields, url will be created only with help of parent_lookup_kwargs.
Should make OneToOne relation's hyper-links auto-creation easier.