python - Django: One-to-many relation for [0, 1] cardinality -


imagine having 2 models:

class service(models.model):   key_service_name = models.foreignkey(key_service_type, related_name='service_key_service_names', null=false)   service_hourly_wage_rate = models.decimalfield(max_digits=5, decimal_places=2, null=true)   service_amount = models.decimalfield(max_digits=7, decimal_places=2, null=true)  class serviceadditionalinfo(models.model):   service = models.foreignkey(service, related_name='service_additional_info_services', null=false)   fixed_price = models.decimalfield(max_digits=8, decimal_places=2, null=true) 

amongst other info, service class serializer features field description:

service_additional_info = serviceadditionalinfoserializer(read_only = true, many=false, source = 'service_additional_info_services') 

in practice, 1 service instance may referenced 0 or 1 serviceadditionalinfo instance. serializer understandably returns list while prefer dictionary.

my question: recommended way of modelling relation? if so, there django-built-in mechanism return dict such cases?

for latter case know how work around issue, since use framework in intended way, i'm curious if there's missed regarding modelling , serializers.

replace foreign key onetoonefield

a one-to-one relationship. conceptually, similar foreignkey unique=true, “reverse” side of relation directly return single object.

this prefereble way of mapping relationship when there can 1 or 0 objects related each other.

as generating single dictionary contains data both models, need subclass model serializer.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -