Skip to content
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

Not Working With @proeprty #83

Closed
bahaasamoudi opened this issue Apr 27, 2020 · 2 comments
Closed

Not Working With @proeprty #83

bahaasamoudi opened this issue Apr 27, 2020 · 2 comments

Comments

@bahaasamoudi
Copy link

bahaasamoudi commented Apr 27, 2020

i have this model

class CachedEvent(models.Model):
      key = models.CharField(max_length=255)
      timestamp = models.DateTimeField()
      eventname = models.CharField(max_length=255)

@property
def priority(self):
    eventtype, created = EventType.objects.get_or_create(
        eventname=self.eventname
    )
    return eventtype.priority

viewSet"

class EventViewSet(viewsets.ReadOnlyModelViewSet):
       queryset = CachedEvent.objects.all()
       serializer_class = CachedEventSerializer

serializer:

class CachedEventSerializer(serializers.ModelSerializer):
     id = serializers.CharField(source='original_id')

     class Meta:
        model = CachedEvent
        fields = ['key', 'timestamp', 'id', 'eventname', 'priority']

it show error "Cannot resolve keyword 'priority' into field. Choices are: eventname, id, key, timestamp "

@izimobil
Copy link
Owner

izimobil commented Jun 3, 2020

You can't use sorting or filtering for dynamic properties, just disable sorting and search for this column in your JS code (see: https://datatables.net/reference/option/columns.orderable and https://datatables.net/reference/option/columns.searchable).

@izimobil izimobil closed this as completed Jun 3, 2020
@morenoh149
Copy link
Contributor

morenoh149 commented Aug 27, 2020

is it possible to surface a model's @Property as a value? not looking to order or sort by this, just extra data for informational purposes.

I have something like this, private works but held does not.

# models.py

class Company(models.Model):
    ...fields...
    is_private = models.BooleanField(default=False)

    @cached_property
    def is_held(company):
        ...logic...

# app.js
$("#table").DataTable({
  serverSide: true,
  ajax: "/api/experts/?format=datatables",
  ordering: false,
  pagingType: "full_numbers",
  responsive: true,
  columns: [
    {
      data: "company_private",
      visible: false,
      orderable: false,
      name: "company.is_private",
      defaultContent: "-",
    },
    {
      data: "company_held",
      visible: false,
      orderable: false,
      name: "company.is_held",
      defaultContent: "-",
    },
  ],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants