In Django, how do I update a user profile if a model is added to database?
<p>I'm creating a site where users can upload files. I have my upload form working, and it correctly associates the current user with the uploaded file. Once it's uploaded however, I would like the User Profile model to update the number of files uploaded for that specific user.  I'm using django-allauth with a custom admin model.<br><br></p>

<p>models.py (works to recognize current user)</p>

<pre><code>class ConfigFiles(models.Model):
    user = models.ForeignKey(
    settings.AUTH_USER_MODEL, blank=True, null=True)

    Printer = models.CharField(_('Printer Model'),
    max_length=100, blank=True, null=True, unique=False, help_text="Something like 'i3'")
    printerbrand = models.CharField(_('Printer Brand'),
    max_length=100, blank=True, null=True, unique=False, help_text="Something like 'Prusa'")
    Plastic = models.CharField(_('Plastic'),    
    max_length=40, blank=True, null=True, unique=False, help_text="Something like 'ABS' or 'Nylon'")

    HDConfig = models.FileField(_('High Detail, Slow Speed'), 
    upload_to=UploadedConfigPath, validators=[validate_file_extension], help_text="Displayed as HD on Infinity-Box")
    LDConfig = models.FileField(_('Fast speed, Low Detail'), 
    upload_to=UploadedConfigLDPath, validators=[validate_file_extension], help_text="Displayed as FAST on Infinity-Box")

    pub_date = models.DateTimeField(_('date_joined'), 
    default=timezone.now)
</code></pre>

<p>And here's my custom admin models.py (doesn't auto update configuploaded)</p>

<pre><code>class UserProfile(models.Model):
    user = models.OneToOneField(DemoUser, primary_key=True, verbose_name='user', related_name='profile')

    avatar_url = models.CharField(max_length=256, blank=True, null=True)

    configuploaded = models.IntegerField(_('Number of uploaded Config files'), default=0, unique=False)
    filesuploaded = models.IntegerField(_('Number of uploaded STL files'), default=0, unique=False)


    dob=models.DateField(verbose_name="dob", blank=True, null=True)

    def __str__(self):
        return force_text(self.user.email)

    class Meta():
        db_table = 'user_profile'
</code></pre>

<p><strong>EDIT 1</strong></p>

<p>Here's view.py.</p>

<pre><code>@login_required
def uplist(request):
    if request.method == 'POST':
        form = ConfigUpload(request.POST, request.FILES)
        if form.is_valid():

            comment = form.save(commit=False)
            comment.user = request.user
            #newdoc = ConfigFiles(HDConfig=request.FILES['HD file'])

            comment.save()


            messages.add_message(request, messages.SUCCESS, "Configuration added")    

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse_lazy('list'))
    else:
        form = ConfigUpload()  # A empty, unbound form
    print (request.user.first_name)
    # Load documents for the list page
    documents = ConfigFiles.objects.all()

    # Render list page with the documents and the form
    return render(
        request,
        'visitor/configupload.html',
        {
        'documents': documents,
        'form': form,
        }
    )
</code></pre>

<p>Ideally, whenever the user uploads a file the configuploaded variable increase correspondingly. Is there a simple way to do this within models or should I do if from my views.py?</p>

<p>You can use Post Save Signal in Django</p>

<pre><code>from django.db.models.signals import *
from django.db.models import F

def file_upload_count(sender, instance, created, *args, **kwargs):
    if created:
        UserProfile.objects.filter(user = instance).update(filesuploaded = F('filesuploaded')+1)

post_save.connect(file_upload_count, sender=ConfigFiles)
</code></pre>

<p>Whenever a new file is uploaded this will perform the function defined and will increment the value or whatever u want.</p>

<p>Try adding this code in your models file</p>

python django django-allauth