title: Django - Generating random, unique slug field for each model object
body: <p>I have a Model called <code>ExampleModel</code> in Django, and want each of the model objects to be uniquely identified. Though, I don't want the ID of the object visible to the user in the URL; and so for this reason I want the objects <code>slug</code> to be a unique, randomly generated integer with 8 digits which will go in the views URL. This is different from other questions I've seen because this means not producing a slug string that is based on the model object's name//content itself.</p>
  
  <p>Models.py:</p>
  
  <pre><code>class ExampleModel(models.Model):
      user = models.ForeignKey(UserModel, related_name='examplemodel', on_delete=models.CASCADE, null=True)
      title = models.CharField(max_length=50, verbose_name='Title')
      slug = models.SlugField(unique=True, blank=True, null=True)
  </code></pre>
  
  <p>Currently the value of the slug is null so I don't have to set a default slug for all of the current <code>ExampleModel</code> objects.</p>
  
  <p>This is quite vague understandably, however I haven't been able to find any guides/tutorials that may work for my exact situation.</p>
  
  <p>Thanks for any help/guidance provided</p>
  
  <p><strong>Edit</strong>
  Here's my views.py:</p>
  
  <pre><code>def model_create(request):
      user=request.user.id
      if request.user.is_authenticated:
          try:
              example = request.user.examplemodel
          except ExampleProfile.DoesNotExist:
              example = ExampleProfile(user)
          if request.method == 'POST':
              form = NewForm(request.POST, request.FILES)
              if form.is_valid():
                  form.save()
                  return redirect('/dashboard/')
              else:
                  return render(request, 'create.html', {'form': form})
          else:
              form = NewForm()
              return render(request, 'create.html', {'form': form})
      else:
          return redirect('/users/login/?next=')
  </code></pre>
  
  <p><strong>Edit 2</strong> Models.py (Save method):</p>
  
  <pre><code>def save(self, *args, **kwargs):
          if self.user is None:  # Set default reference
              self.user = UserModel.objects.get(id=1)
          super(ExampleModel, self).save(*args, **kwargs)
  </code></pre>