Django tastypie: resource shows different in verbose request, like in list request

I'm just starting out with django tastypie, and I'm an enthusiast about it. My question is: I'm looking for the same function as in administrator mode: specify for foreignkey fields what to look for in the answer list of other objects and what in the detailed answer.

let's say this is my simple model:

class Location(models.Model): name = models.CharField(max_length=256, blank=True) longitude = models.FloatField(blank=True, default=0.0) latitude = models.FloatField(blank=True, default=0.0) description = models.CharField(max_length=256, blank=True) shortname = models.CharField(max_length=256, blank=True) tooltiptext = models.CharField(max_length=1000, blank=True) locationtype = models.ForeignKey(LocationType, blank=True, null=True) public_anonymous = models.BooleanField(default=False, blank=False, null=False) public_authorized = models.BooleanField(default=False, blank=False, null=False) def __str__(self): return '%s' % (self.name) class Variable(models.Model): abbreviation = models.CharField(max_length=64, unique=True) name = models.CharField(max_length=256, blank=True) unit = models.CharField(max_length=64, blank=True) def __str__(self): return '%s [%s]' % (self.name, self.unit) class Timeseries(models.Model): locationkey = models.ForeignKey(Location) variablekey = models.ForeignKey(Variable) tstypekey = models.ForeignKey(TimeseriesType) def __str__(self): return '%s: %s (%s)' % (self.locationkey.name, self.variablekey.name, self.tstypekey.name) 

and these are my resources for api:

 class LocationResource(ModelResource): class Meta: queryset = Location.objects.all() resource_name = 'location' excludes = ['public_anonymous', 'public_authorized'] authentication = BasicAuthentication() authorization = DjangoAuthorization() class VariableResource(ModelResource): class Meta: queryset = Variable.objects.all() resource_name = 'variable' authentication = BasicAuthentication() authorization = DjangoAuthorization() class TimeseriesTypeResource(ModelResource): class Meta: queryset = TimeseriesType.objects.all() resource_name = 'timeseriestype' authentication = BasicAuthentication() authorization = DjangoAuthorization() class TimeseriesResource(ModelResource): location = fields.ForeignKey(LocationResource, 'locationkey', full=False) variable = fields.ForeignKey(VariableResource, 'variablekey', full=False) timeseriestype = fields.ForeignKey(TimeseriesTypeResource, 'tstypekey', full=False) class Meta: queryset = Timeseries.objects.all() resource_name = 'timeseries' authentication = BasicAuthentication() authorization = DjangoAuthorization() 

In TimeseriesResource, if you use full=False , you just get the url with id, if you use full=True , you get all the information. In a real place there is much more information. I need a little more information than full = 'False', but not all information with full=True . I do not want to use the exclude parameter because I do not have the information in the details or in the list of the Location object.

one of the options I was thinking about is 2 resources for the same object, but that doesn't seem like the best solution (but I think it will work). By the way: I thought about this option, it won’t work (of course), it’s better to use the workaround laid down in bmihelac's answer (thanks).

Despite ... trying a workaround ... leads me to a new question, see:

django-tastypie: cannot access bundle.request in dehydrate (self, bundle)

+6
source share
3 answers

There is a function request for different fields in show and index , along with some discussions on how this can be implemented:

https://github.com/toastdriven/django-tastypie/issues/18

While this feature is not enabled, you may be able to help in this workaround:

https://github.com/toastdriven/django-tastypie/issues/18#issuecomment-2695447

+5
source

Good news! This has been added to tastypie.

If you want to customize the field only on certain pages, simply define the use_in attribute as β€œall,” β€œlist,” or β€œpart.”

More details here , but I will give an example for posterity:

 class DocumentResource(ModelResource): my_field = fields.CharField(attribute='my_field', use_in='detail') 

Just!

+5
source

By the way, I switched to another restfull django api framework by looking at http://django-rest-framework.org/ . Therefore, I no longer use tastipia. However, this question and answer is probably still useful for others.

I found the django-rest-framework a lot easier and more flexible to implement it. I did not find anything that I could not do with this framework (well, this does not bring me coffee).

0
source

Source: https://habr.com/ru/post/902199/


All Articles