SHARE
TWEET

Untitled

a guest May 21st, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. === MODELS ===
  2. class Person(models.Model):
  3.     name = models.CharField(max_length=32, blank=False)
  4.     creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  5.     groups = models.ManyToManyField(PersonGroup)
  6.  
  7.     def __str__(self):
  8.         return self.name
  9.  
  10. class PersonContact(models.Model):
  11.     TYPE_VKONTAKTE='VK'
  12.     TYPE_PHONE='PH'
  13.     TYPE_TELEGRAM='TG'
  14.     TYPE_CHOICES = (
  15.         (TYPE_VKONTAKTE, 'VKontakte'),
  16.         (TYPE_PHONE, 'Mobile phone'),
  17.         (TYPE_TELEGRAM, 'Telegram'),
  18.     )
  19.  
  20.     type = models.CharField(max_length=2, choices=TYPE_CHOICES)
  21.     value = models.CharField(max_length=32, blank=False)
  22.     person = models.ForeignKey(Person, on_delete=models.CASCADE)
  23.  
  24.     def __str__(self):
  25.         return self.name
  26.  
  27. === FORMS ===
  28. class PersonForm(ModelForm):
  29.     groups_select = forms.ModelMultipleChoiceField(queryset=None)
  30.  
  31.     def __init__(self, user, *args, **kwargs):
  32.         super(PersonForm, self).__init__(*args, **kwargs)
  33.         self.fields['groups_select'].queryset = PersonGroup.objects.filter(creator = user)
  34.  
  35.     class Meta:
  36.         model = Person
  37.         fields = ('name',)
  38.        
  39. ContactFormset = inlineformset_factory( Person,
  40.                                         PersonContact,
  41.                                         fields=('type', 'value'),
  42.                                         extra=1,
  43.                                         )
  44.  
  45. === VIEWS ===
  46.  
  47. @login_required
  48. def personal_person_add(request):
  49.     if request.method == 'POST':
  50.         person_form = PersonForm(request.user, request.POST or None)
  51.         newperson = person_form.instance
  52.         newperson.creator = request.user
  53.         if person_form.is_valid():  
  54.             form = person_form.save(commit=False)
  55.             contacts_form = ContactFormset(request.POST, instance=form)
  56.             if contacts_form.is_valid():
  57.                 person_form.save()
  58.                 contacts_form.save()
  59.             return redirect('persons')
  60.     else:
  61.         contacts_form = ContactFormset()
  62.         person_form = PersonForm(request.user)
  63.  
  64.     return render(
  65.         request,
  66.         'app/form_person.html',
  67.         {
  68.            'form_person': person_form,
  69.            'form_contacts': contacts_form,
  70.         }
  71.     )
  72.  
  73. === TEMPLATE ===
  74.             {{form_person}}
  75.             {{form_contacts.management_form}}
  76.             {% for form in form_contacts %}  
  77.                 <div class="link-formset">
  78.                     {{ form }}    
  79.                 </div>
  80.             {% endfor %}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top