GithubHelp home page GithubHelp logo

Comments (3)

kooba avatar kooba commented on May 23, 2024

Looks like it was pretty straightforward after all. Leaving this for future reference.

All I had to do was to:

add code property to oscar's AbstractLineAttribute:

class AbstractLineAttribute(models.Model):
    (...)
    @property
    def code(self):
        return self.option.code

and code serializers.CharField to oscarapi's LineAttributeSerializer

class LineAttributeSerializer(OscarHyperlinkedModelSerializer):
    code = serializers.CharField(max_length=128)

and then adding code to fields in BasketLineSerializer

class BasketLineSerializer(OscarHyperlinkedModelSerializer):
    (...)
    attributes = LineAttributeSerializer(
        many=True, fields=('url', 'option', 'value', 'code'),
        required=False, read_only=True)
   (...)

from django-oscar-api.

maerteijn avatar maerteijn commented on May 23, 2024

Hi @kooba

You're solution is indeed ok and it is documented like this in the djangorestframework documentation.

However, to solve this properly you need to fork the basket app from oscar just to override the LineAttribute model (https://django-oscar.readthedocs.io/en/releases-1.5/topics/fork_app.html), and you needed to override oscarapi's serializers.

You could solve it as well by just overriding oscarapi's serializers (see https://django-oscar-api.readthedocs.io/en/latest/usage/customizing_oscarapi.html), and do something like this:

from oscarapi.serializers import LineAttributeSerializer, BasketLineSerializer


class MyLineAttributeSerializer(LineAttributeSerializer):
    code = serializers.SerializerMethodField()

    def get_code(self, obj):
        return obj.option.code


class MyBasketLineSerializer(BasketLineSerializer):
    attributes = LineAttributeSerializer(
        many=True, fields=('url', 'option', 'value', 'code')
        required=False, read_only=True)

Good luck!

from django-oscar-api.

kooba avatar kooba commented on May 23, 2024

Thank you!

Yes of course, all the changes were applied on the fork as described in oscar api customization docs.

from django-oscar-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.