Django ModelForm Custom Autofill Confirmation for M2M, Instead of Ugly Multi-Select

Given the following models (shortened for understanding):

class Venue(models.Model):
    name = models.CharField(unique=True)

class Band(models.Model):
    name = models.CharField(unique=True)

class Event(models.Model):    
    name = models.CharField(max_length=50, unique=True)       
    bands = models.ManyToManyField(Band) 
    venue = models.ForeignKey(Venue)
    start = models.DateField()
    end = models.DateField()

The admin area works great for what I'm doing, but I would like to open the site a bit so that some users can add new events. For public parts, I have several “administrative” fields on these models that I don’t want to see the public (which is easy enough to fix).

, , ManyToMany . , - , , , (, "", , StackOverflow!).

, Band.id, . , , , Django ModelForms, - "Bands".

, , StackOverflow, - - , Django , , .., .

Auto-Complete ? - , ?

AutoComplete, Autocomplete, , .

/, , Wall Of Text. , .

+3
2

, , , , <select>, ModelForm .

, POST :

name=FooBar2009&bands=1&bands=3&bands=4&venue=7&start=...

Javascript ( ) , band, . , , , ModelForm .

+1

jQuery,

- :

jQuery("#id_tags").autocomplete('/tagging_utils/autocomplete/tasks/task/', {
    max: 10,
    highlight: false,
    multiple: true,
    multipleSeparator: " ",
    scroll: true,
    scrollHeight: 300,
    matchContains: true,
    autoFill: true,
}); 

, , a:

http://skyl.org/tagging_utils/autocomplete/tasks/task/?q=a&limit=10×tamp=1259652876009

, :

http://github.com/skyl/skyl.org/blob/master/apps/tagging_utils/views.py

, .. POST, .get() , ... right, name =... unique = True.. - ( )...:

def clean_bands(self):
    return Band.objects.filter( name__in = self.cleaned_data['bands'].split(' ') )

, . , qs. , , , /.

0

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


All Articles