Итак, у меня есть компонент обмена сообщениями в приложении, которое я создаю. Существует объект Conversation
, который имеет поле ManyToMany для пользователя. Затем есть фактический InsantMessage
объект, который имеет поле отправителя для пользователя, текстовое поле и другое поле с именем receiver
, которое является внешним ключом для моего объекта разговора. Теперь я пытаюсь создать ссылку под профилем другого пользователя, а затем передать свой pk в этой ссылке в мое представление, которое сначала проверит, существует ли уже объект диалога с request.user и другим пользователем, и если это так, создаст объект сообщения и затем автоматически передаст объект Convo pk для поля внешнего ключа conversation
в моем объекте сообщения. Теперь я думаю, что у меня есть базовое понимание того, как это сделать, возможно, просто используя оператор if и вложенную форму. Но где я застрял в том, как я могу получить pk разговора, чтобы автоматически пройти? И как бы, если объект разговора создается, как я могу заставить request.user / другого пользователя автоматически помещаться в объект разговора? Даже если вы не хотите писать мой код, просто укажите мне, где искать. Я застрял на этой проблеме в течение нескольких дней.
models.py
class ProfileManager(BaseUserManager):
def create_user(self, username, email,description,photo, password=None):
if not email:
raise ValueError("You must creat an email")
if not username:
raise ValueError("You must create a username!")
if not description:
raise ValueError("You must write a description")
if not photo:
raise ValueError("You must upload a photo")
user = self.model(
email=self.normalize_email(email),
username = username,
description= description,
photo= photo,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email,description,photo, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
description=description,
photo=photo,
)
user.is_admin=True
user.is_staff=True
user.is_superuser=True
user.save(using=self._db)
return user
class Profile(AbstractBaseUser):
class Meta:
swappable = 'AUTH_USER_MODEL'
email = models.EmailField(verbose_name="email")
username = models.CharField(max_length=30, unique=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
#what I added
description = models.TextField()
photo = models.ImageField(upload_to='profile_photo',blank=False, height_field=None, width_field=None, max_length=100)
matches = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='+', blank=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['description','photo','email']
objects = ProfileManager()
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self,app_label):
return True
class UserVote(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
voter = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='given_vote', on_delete=models.CASCADE)
vote = models.BooleanField(default=False)
class Meta:
unique_together = (('user', 'voter'))
class Conversation(models.Model):
members = models.ManyToManyField(settings.AUTH_USER_MODEL)
class InstantMessage(models.Model):
sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name= 'senderr',on_delete=models.CASCADE )
receiver = models.ForeignKey(Conversation, on_delete=models.CASCADE)
message = models.TextField()
date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.message
#tests to see if messages are exclusive between sender, receiver (won't work with new model)
@classmethod
def find_messages_exclusive_to_profile(cls,sender,receiver):
#members = receiver AND sender, not receiver or sender
exclusive_conversations = Conversation.objects.filter(members= receiver ).filter(members= sender)
exclusive_messages = InstantMessage.objects.filter(receiver__in=exclusive_conversations)
return exclusive_messages
views.py
def home(request):
context = {'random_profiles': Profile.objects.exclude(id=request.user.id).order_by('?')[:3]}
return render(request, 'dating_app/home.html',context)
def profiles(request):
"Shows a list of profiles that have been created"
profiles = Profile.objects.order_by('date_joined')
context = {'profiles' : profiles}
return render(request, 'dating_app/profiles.html',context)
def profile(request, profile_id):
"""show a single profile"""
profile = get_object_or_404(Profile,id=profile_id)
context = {'profile' : profile}
return render(request, 'dating_app/profile.html', context)
# Below is related to users
def logout_view(request):
"""Log out the user """
logout(request)
return HttpResponseRedirect(reverse('dating_app:home'))
def register(request, profile_id):
user =User.objects.get(pk=profile_id)
user.profile.bio = 'fjfjfjjf'
user.save()
def register(request):
#Register a new user
context = {}
if request.POST:
form = RegistrationForm(request.POST, request.FILES)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
email = form.cleaned_data.get("email")
description = form.cleaned_data.get("description")
photo = form.cleaned_data.get("photo")
raw_password = form.cleaned_data.get('password1')
profile = authenticate(username=username,email=email,description=description, photo=photo, password=raw_password)
login(request, profile)
return redirect ('dating_app:home')
else:
context['registration_form'] = form
else: #get request
form = RegistrationForm()
context['registration_form'] = form
return render(request, 'dating_app/register.html', context)
def update_account(request, profile_id):
#Edit an existing profile
profile = get_object_or_404(Profile,id=profile_id)
update_form = ProfileUpdateForm(request.POST, request.FILES)
if request.method != 'POST':
#Initial request; prefil form with current entry
update_form = ProfileUpdateForm(instance=profile)
else:
#POST data submitted;process data.
update_form = ProfileUpdateForm(instance=profile, data=request.POST, files=request.FILES)
if update_form.is_valid():
update_form.save()
return HttpResponseRedirect(reverse('dating_app:profile', args=[profile.id]))
context = {'profile' : profile, 'update_form' : update_form}
return render(request, 'dating_app/update.html', context)
#matching
def mingle(request):
try:
profile = (Profile.objects.exclude(id=request.user.id).exclude(uservote__voter=request.user).order_by('?')[0])
except IndexError:
profile = None
print(Profile.username)
try:
description = request.user.description
except Profile.DoesNotExist:
create = Profile.objects.get_or_create(request.user)
return redirect('profile')
match = request.user.matches.all()
context = dict(profile = profile, match = match)
return render(request, 'dating_app/mingle.html', context)
@login_required
def nice(request, profile_id):
return create_vote(request, profile_id, True)
@login_required
def nope(request, profile_id):
return create_vote(request, profile_id, False)
def create_vote(request, profile_id, vote):
profile = get_object_or_404(Profile, pk=profile_id)
UserVote.objects.create(
user=profile,
voter=request.user,
vote=vote
)
other = UserVote.objects.filter(
voter=profile,
user=request.user,
vote=True
)
if vote and other.exists():
profile.matches.add(request.user)
request.user.matches.add(profile)
return redirect('dating_app:mingle')
def view_matches(request,profile_id):
match = request.user.matches.all()
profile = get_object_or_404(Profile,id=profile_id)
context = {'match' : match, 'profile' : profile}
return render(request, 'dating_app/matches.html', context)
#form
def instant_message(request, receiver_id):
if request.method == 'POST':
form = InstantMessageForm(request.POST)
if form.is_valid():
form.instance.sender = request.user
form.instance.receiver = get_object_or_404(get_user_mode(), pk=receiver_id)
form.save()
return redirect('dating_app:home')
else:
form = InstantMessageForm()
context = {'form':form}
return render(request, 'dating_app/instant_message_form.html',context)
def messages(request, profile_id):
other_user = get_object_or_404(Profile,id=profile_id)
exclusive_conversations = Conversation.objects.filter(members= request.user ).filter(members= other_user)
messages = InstantMessage.objects.filter(receiver__in=exclusive_conversations)
context = {'messages' : messages, 'other_user' : other_user}
return render(request, 'dating_app/messages.html', context)
def conversations(request,profile_id):
conversations = Conversation.objects.filter(
members=profile_id
).annotate(
last_message=Max('instantmessage__date')
).prefetch_related('members').order_by(
'-last_message'
)
return render(request, 'dating_app/conversations.html', {'conversations':conversations,})