0
1

Hello All,

I am very new to django. My model is like below

   class APIEntry(models.Model):
        version = models.ForeignKey(Version)
        name=models.CharField(max_length=20,unique=True)
        url=models.URLField()
        mimetype=models.CharField(max_length=20,default='application/json')
        method=models.CharField(max_length=50)
        content=models.TextField()

i need to implement auto complete for the Mime Field with the 5 predefined values (text/plain, text/html, application/xml, application/octet stream,multipart/mixed). I googled a lot and i am unable to find the correct solution for this. can anybody please sugget me how to implement this?

Thanks in advance

asked 17 Aug '13, 06:26

navula's gravatar image

navula
111
accept rate: 0%


You should add a get mimetype_defaults(self) method to your model, that will return default mimetypes. Then in your forms and views, you may display these values as predefined.

However, if you ever plan on the users / administrators of the site to be able to change them, it's better to do it in the database then. Make the mimetype field a ForeignKey to a custom Mimetype model. This model will have a default BooleanField that you may set as True in its fields. Then in your form logic, you only have to fetch the mimetypes from the Mimetype model (Mimetype.objets.filter(default=True)) as pre-defined values for your field.

The second option has the benefit of making your code depend on database data instead of hard-coded values.

link

answered 17 Aug '13, 20:48

sebleblanc's gravatar image

sebleblanc
111
accept rate: 0%

Hi sebleblanc thank u so much for ur reply. I am not using any forms and views actually here. I am just using the Django Admin to take care about the crud operations. in the djangoa dmin for the field mimetype i need autocomplete with the some predefined values...look like ur 1st solution is simple but i am unable to implement,i am very glad if u send me the method which i need to add...Thanks

(19 Aug '13, 00:35) navula

I am curious: are you looking to have the possibility of entering a custom value, along with default "suggested" values? This is commonly called a combobox, and unfortunately, Django does not have native support for these.

Fields in Django can have a choices argument, that is either an iterable of possible values, or a callable. Therefore you may write this: mimetype = CharField(max_length=20, default='application/json', choices=['application/json', 'text/html', 'image/png', ... ]) This will limit the possible values at validation, right before saving the model into the database.

(19 Aug '13, 00:54) sebleblanc

This however ties your code with your data, which some might find inadequate. Furthermore, it does not allow you to easily add other allowed mimetypes without updating the code, and if someone ever add an "invalid" value in the database, updating this entry in Django will force you to change the value to an accepted one, or you may face validation errors.

(19 Aug '13, 00:56) sebleblanc
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×2
×1
×1
×1

Asked: 17 Aug '13, 06:26

Seen: 562 times

Last updated: 19 Aug '13, 00:56

powered by OSQA