DRF regex search on all fields - django

I need DRF '$'- regex search on all("__all__") fields of a model in Django Rest API Framework.
I can specify like search_fields = ['$username', '$first_name', '$email', '$last_name'] this on all fields explicitly on a model.
but I need $-regex search on all fields of a model something like search_fields = '$__all__'.
Please anybody giude me on this , Thanks in advance.

If you want a list of all the fields in a model, you can use
[field.name for field in model._meta.get_fields()]
You can add checks while building up the list of the fields:
[field.name for field in model._meta.get_fields() if field.name.startswith("$")]

Related

Django: multiple different fields -> one model

I want to show different fields (a html-option-field who gets Mymodel.object.all and a textfield) and save it to one model field.
How can I build this?
MultiValueField (https://docs.djangoproject.com/en/2.1/ref/forms/fields/) doesn't help with different fields?
Has someone an example? How can I define which kind of field it is?
EDIT:
How can I determine which field I want to save in the model-field? I use a ModelForm.
You should use forms.ModelChoiceField(choices=ModelClass.objects.all()) for the choicefield, you can also set the widget to be widget=forms.CheckboxSelectMultiple.
your form can be like
class SuperForm(forms.Form):
cool_field = forms.ModelChoiceField(
choices=ModelClass.objects.all(),
widget=forms.CheckboxSelectMultiple,
)
text_area = forms.CharField(widget=forms.Textarea)

access model fields and attributes in django

I'm writing a simple scaffolding app in django, however I'm still not able to access a models fields and attributes(e.g. CharField, max_length=100, null=True, etc...). I know about the _meta class of models, but as far as I know it only retrieves basic info about the model not the fields. Is there a away to achieve this?
Update:
you can find the answer in this article:
http://www.b-list.org/weblog/2007/nov/04/working-models/
You should use the get_field method to get info for a particular field:
field = ModelName._meta.get_field('field_name')
Then you may check various attributes of field, like field.blank, field.null, field.name etc.
If on the other hand you want to get a list of all fields of a Model you should use fields:
fields = ModelName._meta.fields
For example to get the name of all your model fields you can do something like:
field_names = ', '.join(f.name for f in fields)
Hm... also I just noticed that your question is a duplicate of Get model's fields in Django !

django rest framework - add ManyToMany relationship

I'm having trouble adding to tag field through many to many relationship on article object.
I'm able to replace all current article.tags by passing HTTP method of PATCH.
However, I would like to add to the existing relationships on article.tags, rather than replacing.
I cannot find a built in way to do this. Am I missing something obvious?
My article modedel serializer has the following field:
tags = serializers.PrimaryKeyRelatedField(many=True)
In your 'article' model serializer instead of
tags = serializers.PrimaryKeyRelatedField(many=True)
use the below code
tags = TagSerializer()
here 'TagSerializer' is the model serializer of 'Tag' model like
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag

Textual search for ForeignKey field in Django Admin

I apologize if this question has already been answered, but I couldn't find it anywhere else.
In my django project, I have foreignkey fields which have hundreads of options for selection.
The django dropdown default widget for foreignkey fields makes it really difficult to find the one I want.
I have crossed with the filter_horizontal admin option, which brings great selection functionality for many to many fields with a textual search field and two selectors.
I was wondering if there is a django built-in option or if any of you has found a solution that allows me do a textual search "on-the-fly" for foreignkey fields as in many to many fields specified in "filter_horizontal"
Thanks in advance.
Check out raw_id_fields
Old question, but if anyone lands here: Today we can use autocomplete_fields like this:
class MyRelatedClassAdmin(admin.ModelAdmin):
search_fields = ['name']
class MyClassAdmin(admin.ModelAdmin):
autocomplete_fields = ('related', )
admin.site.register(MyClass, MyClassAdmin)
admin.site.register(MyRelatedClass, MyRelatedClassAdmin)
where MyClass has a ForeignKey-field named related. The name field of the related model will be used.

Putting 2 fields in the same column in the Django Admin?

Two of my model's fields are "title" and "summary". Right now, "title" is the first field in the ModelAdmin's list_display, which makes it link to the change page. I have some other fields in list_display as well.
I'd like to make the admin change list page display "summary" under "title" in plain, unlinked text, in the same column as "title". Is this possible? I'm using Django 1.1.
Thanks
Kind of. You can setup your own custom list_display objects to use. So for example, in your case you may do something like so:
def title_and_summary(obj):
return "%s %s" % (obj.title, obj.summary)
Then within your admin class:
class MyAdmin(admin.ModelAdmin):
list_display = (title_and_summary,)
More information can be found on the list_display documentation.