10 examples of 'django queryset get field value' in Python

Every line of 'django queryset get field value' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
276def get_values(self):
277 values = []
278 if isinstance(self.model_field, SmartListFilter):
279 values = [
280 SmartFilterValue(self.model_field.parameter_name, choice[1], choice[0], self.query_params)
281 for choice in self.model_field.lookups()
282 ]
283 elif self.model_field.choices:
284 values = [
285 SmartFilterValue(self.field_name, choice[1], choice[0], self.query_params)
286 for choice in self.model_field.choices
287 ]
288 elif type(self.model_field) == BooleanField:
289 values = [
290 SmartFilterValue(self.field_name, choice[1], choice[0], self.query_params)
291 for choice in ((1, _('Yes')), (0, _('No')))
292 ]
293 elif issubclass(type(self.model_field), ForeignKey):
294 pks = self.object_list.order_by().distinct().values_list('%s__pk' % self.field_name, flat=True)
295 remote_field = self.model_field.rel if hasattr(self.model_field, 'rel') else self.model_field.remote_field
296 qs = remote_field.model.objects.filter(pk__in=pks)
297 values = [SmartFilterValue(self.field_name, obj, str(obj.pk), self.query_params) for obj in qs]
298
299 return [SmartFilterValue(self.field_name, _("All"), None, self.query_params)] + values
22def get_prep_lookup(self):
23 if django.VERSION >= (1, 10):
24 if hasattr(self.rhs, '_prepare'):
25 value = self.rhs._prepare(self.lhs.output_field)
26 else:
27 value = self.rhs
28 value = self.lhs.output_field.get_prep_value([value])
29 else:
30 value = self.lhs.output_field.get_prep_lookup(
31 self.lookup_name, [self.rhs]
32 )
33 return value
27def get_queryset(self, resolved_qs, args, info):
28 return resolved_qs
71def get_field_value(self, instance, field_name):
72 return instance.query_string
28@classmethod
29def filter_for_lookup(cls, field, lookup_type):
30 if isinstance(field, EnumChoiceField):
31 return EnumChoiceFilter, {
32 'enum_class': field.enum_class,
33 'choice_builder': field.choice_builder
34 }
35
36 return super().filter_for_lookup(field, lookup_type)
80def value_from_object(self, instance):
81 return getattr(instance, self.name)
173def prepare_value(self, obj):
174 # Called when preparing the ChoiceField widget from the to_model queryset.
175 return obj.serializable_value('id')
102def get_prep_lookup(self, lookup_type, value):
103 if hasattr(value, "value"):
104 value = value.value
105
106 return super(SluggableField, self).get_prep_lookup(lookup_type, value)
80def get_queryset(self, request):
81 qs = super(RelatedFieldAdmin, self).get_queryset(request)
82
83 # include all related fields in queryset
84 select_related = [field.rsplit('__', 1)[0] for field in self.list_display if is_field_allowed(field)]
85
86 # explicitly add contents of self.list_select_related to select_related
87 if self.list_select_related:
88 for field in self.list_select_related:
89 select_related.append(field)
90
91 # Include all foreign key fields in queryset.
92 # This is based on ChangeList.get_query_set().
93 # We have to duplicate it here because select_related() only works once.
94 # Can't just use list_select_related because we might have multiple__depth__fields it won't follow.
95 model = qs.model
96 for field_name in self.list_display:
97 try:
98 field = model._meta.get_field(field_name)
99 except models.FieldDoesNotExist:
100 continue
101 try:
102 remote_field = field.remote_field
103 except AttributeError: # for Django<1.9
104 remote_field = field.rel
105 if isinstance(remote_field, models.ManyToOneRel):
106 select_related.append(field_name)
107
108 return qs.select_related(*select_related)
23def get_field(self, field_name):
24
25 try:
26 field = self.opts.get_field(field_name)
27 return field.name
28 except FieldDoesNotExist:
29 if callable(field_name):
30 attr = field_name
31 elif hasattr(self, field_name):
32 attr = getattr(self, field_name)
33 else:
34 attr = getattr(self.model, field_name)
35 return getattr(attr, '__name__', None)

Related snippets