1. Mark Lavin
  2. django-selectable

Commits

Mark Lavin  committed 18f280d

User auto-complete which creates new users.

  • Participants
  • Parent commits 80384d3
  • Branches default

Comments (0)

Files changed (2)

File example/core/admin.py

View file
 from django.contrib import admin
+from django.contrib.auth.models import User
 from django import forms
 
 import selectable.forms as selectable
 
 
 class FarmAdminForm(forms.ModelForm):
-    owner = selectable.AutoComboboxSelectField(lookup_class=OwnerLookup)
+    owner = selectable.AutoComboboxSelectField(lookup_class=OwnerLookup, allow_new=True)
     fruit = selectable.AutoCompleteSelectMultipleField(lookup_class=FruitLookup)
 
     class Meta(object):
         model = Farm
 
+    def clean(self, *args, **kwargs):
+        owner = self.cleaned_data['owner']
+        if owner and not owner.pk:
+            owner = User.objects.create_user(username=owner.username, email='')
+            self.cleaned_data['owner'] = owner
+        return self.cleaned_data
+        
 
 class FarmAdmin(admin.ModelAdmin):
     form = FarmAdminForm

File example/core/models.py

View file
     owner = models.ForeignKey('auth.User', related_name='farms')
     fruit = models.ManyToManyField(Fruit)
 
+    def __unicode__(self):
+        return u"%s's Farm: %s" % (self.owner.username, self.name)