AttributeError: module ‘delivery.models.order_timelocation’ has no attribute ‘_meta’

I'm new into django and I'm trying to do some one to many relationships. In my project I have the following models:

class Order(models.Model):
    customer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='customer')
    retailer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='retailer')
    date_publish = models.DateField()
    date_available = models.DateField()
    weight = models.DecimalField(decimal_places=2, max_digits=5)

class orderTimelocation(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='order_timelocation'),
    longitude = models.DecimalField(decimal_places=8, max_digits=12)
    latitude = models.DecimalField(decimal_places=8, max_digits=12)

class timeInterval(models.Model):
    start = models.DateTimeField()
    end = models.DateTimeField()
    order_timelocation = models.ForeignKey(orderTimelocation, related_name='time_interval', on_delete=models.CASCADE)

and the following serializers:

class OrderSerializer(serializers.ModelSerializer):
    orderTimeLocations = orderTimelocationSerializer(many=True)

    class Meta:
        model = Order
        fields = ['customer', 'retailer', 'date_publish', 'date_available', 'weight', 'orderTimeLocations']

class orderTimelocationSerializer(serializers.ModelSerializer):
    timeintervals = timeIntervalSerializer(many= True)

    class Meta:
        model = order_timelocation
        fields = ('longitude', 'latitude', 'timeintervals')

class timeIntervalSerializer(serializers.ModelSerializer):
    class Meta:
        model = time_interval
        fields = ['start', 'end']

I tried to follow the example given in django rest framework documentation on the topic Nested relationships, that I think is the most appropriated for this case, but the following error occurs

Traceback (most recent call last):
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/generics.py", line 190, in post
    return self.create(request, *args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/mixins.py", line 18, in create
    serializer.is_valid(raise_exception=True)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 220, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 419, in run_validation
    value = self.to_internal_value(data)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 476, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 607, in run_validation
    value = self.to_internal_value(data)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 643, in to_internal_value
    validated = self.child.run_validation(item)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 419, in run_validation
    value = self.to_internal_value(data)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 472, in to_internal_value
    for field in fields:
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 355, in _writable_fields
    for field in self.fields.values():
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 349, in fields
    for key, value in self.get_fields().items():
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 1028, in get_fields
    info = model_meta.get_field_info(model)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/utils/model_meta.py", line 35, in get_field_info
    opts = model._meta.concrete_model._meta
AttributeError: module 'delivery.models.order_timelocation' has no attribute '_meta'

I've seen a couple of questions here but none of the answers to those questions solves my problem.



Read more here: https://stackoverflow.com/questions/68468434/attributeerror-module-delivery-models-order-timelocation-has-no-attribute-m

Content Attribution

This content was originally published by miguel__frtt at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: