Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

How to customise unique = true in django

ok basically, users on my website can create blogposts. But I do not want to use unique = True because that'll mean that user1 cannot create a post that has the same title as user2.

I want to make it such that user1 can create a post that has the same title as user2 BUT user1 CANNOT create another post with the same name as any existing post that USER1 has previously created.

Meaning user 1 can have a blogpost title "hello" and user 2 can also have a blogpost "hello" but user 1 cannot have 2 blogpost with the same title 'hello'

Ok i think this is clear enough^....if I use unique = true, this would be too much, so how can I customise that such that it only applies to the user's post? Thanks!

models.py

class BlogPost(models.Model):
 title                  = models.CharField(max_length=50, null=False, blank=False, unique=True)
 author                     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
 slug                   = models.SlugField(blank=True, unique=True)

class Account(AbstractBaseUser):
 email                  = models.EmailField(verbose_name="email", max_length=60, unique=True)
 username               = models.CharField(max_length=30, unique=True)

forms.py

class CreateBlogPostForm(forms.ModelForm):
    class Meta:
        model = BlogPost
        fields = ['chief_title']

html

   <form class="create-form" method="post" enctype="multipart/form-data">{% csrf_token %}
   <div class="form-group">
    <label for="id_title">Title</label>
    <input class="form-control" type="text" name="title" id="id_title" placeholder="Title" required autofocus>
   </div>
   <button class="submit-button btn btn-lg btn-primary btn-block" type="submit">Submit</button>

  </form>

  {% if form.errors %}
  {% for field in form %}
      {% for error in field.errors %}
          <div class="alert alert-danger">
              <strong>{{ error|escape }}</strong>
          </div>
      {% endfor %}
{% endif %}
    

views.py

def create_blog_view(request):
    context = {}
    user = request.user
    if request.method == 'POST':
        form = CreateBlogPostForm(request.POST or None, request.FILES or None)    
        if form.is_valid():
            obj.save()
            return redirect('HomeFeed:main')
        else:
            context['form'] = form
    return render(request, "HomeFeed/create_blog.html", context)

question from:https://stackoverflow.com/questions/65935571/how-to-customise-unique-true-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you could use a unique constraint, this allows 2 fields to interact and be unique so you could have a post name that can only be used once per user


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...