Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djangoldp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
djangoldp-packages
djangoldp
Commits
db8a1860
Commit
db8a1860
authored
5 years ago
by
Jean-Baptiste
Browse files
Options
Downloads
Patches
Plain Diff
fix
#145
manage empty value on standard_field
parent
dfc52ce4
No related branches found
No related tags found
1 merge request
!72
Resolve "DateField shouldn't be mandatory"
Pipeline
#967
passed
5 years ago
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
djangoldp/serializers.py
+16
-3
16 additions, 3 deletions
djangoldp/serializers.py
djangoldp/tests/models.py
+2
-0
2 additions, 0 deletions
djangoldp/tests/models.py
djangoldp/tests/tests_save.py
+2
-1
2 additions, 1 deletion
djangoldp/tests/tests_save.py
with
20 additions
and
4 deletions
djangoldp/serializers.py
+
16
−
3
View file @
db8a1860
...
...
@@ -253,13 +253,26 @@ class LDPSerializer(HyperlinkedModelSerializer):
obj
=
next
(
filter
(
lambda
o
:
not
hasattr
(
o
,
self
.
parent
.
url_field_name
)
or
"
./
"
in
o
[
self
.
url_field_name
],
object_list
))
return
super
().
get_value
(
obj
)
value
=
super
().
get_value
(
obj
)
else
:
resource_id
=
Model
.
resource_id
(
self
.
parent
.
instance
)
obj
=
next
(
filter
(
lambda
o
:
resource_id
.
lstrip
(
'
/
'
)
in
o
[
self
.
parent
.
url_field_name
],
object_list
))
return
super
().
get_value
(
obj
)
value
=
super
().
get_value
(
obj
)
except
KeyError
:
return
super
().
get_value
(
dictionary
)
value
=
super
().
get_value
(
dictionary
)
return
self
.
manage_empty
(
value
)
def
manage_empty
(
self
,
value
):
if
value
==
''
and
self
.
allow_null
:
# If the field is blank, and null is a valid value then
# determine if we should use null instead.
return
''
if
getattr
(
self
,
'
allow_blank
'
,
False
)
else
None
elif
value
==
''
and
not
self
.
required
:
# If the field is blank, and emptiness is valid then
# determine if we should use emptiness instead.
return
''
if
getattr
(
self
,
'
allow_blank
'
,
False
)
else
empty
return
value
field_class
,
field_kwargs
=
super
().
build_standard_field
(
field_name
,
model_field
)
field_kwargs
[
'
parent_view_name
'
]
=
'
{}-list
'
.
format
(
model_field
.
model
.
_meta
.
object_name
.
lower
())
...
...
This diff is collapsed.
Click to expand it.
djangoldp/tests/models.py
+
2
−
0
View file @
db8a1860
...
...
@@ -57,6 +57,8 @@ class LDPDummy(Model):
class
Invoice
(
Model
):
title
=
models
.
CharField
(
max_length
=
255
,
blank
=
True
,
null
=
True
)
date
=
models
.
DateField
(
blank
=
True
,
null
=
True
)
class
Meta
:
depth
=
2
...
...
This diff is collapsed.
Click to expand it.
djangoldp/tests/tests_save.py
+
2
−
1
View file @
db8a1860
...
...
@@ -15,6 +15,7 @@ class Save(TestCase):
"
@id
"
:
"
./
"
,
"
batches
"
:
{
"
@id
"
:
"
_:b381
"
},
"
title
"
:
"
Nouvelle facture
"
,
"
date
"
:
""
},
{
"
@id
"
:
"
_:b381
"
,
...
...
@@ -28,7 +29,7 @@ class Save(TestCase):
]
}
meta_args
=
{
'
model
'
:
Invoice
,
'
depth
'
:
2
,
'
fields
'
:
(
"
@id
"
,
"
title
"
,
"
batches
"
)}
meta_args
=
{
'
model
'
:
Invoice
,
'
depth
'
:
2
,
'
fields
'
:
(
"
@id
"
,
"
title
"
,
"
batches
"
,
"
date
"
)}
meta_class
=
type
(
'
Meta
'
,
(),
meta_args
)
serializer_class
=
type
(
LDPSerializer
)(
'
InvoiceSerializer
'
,
(
LDPSerializer
,),
{
'
Meta
'
:
meta_class
})
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment