models.py
class Currency(models.Model):
currency_name = models.CharField(max_length=250)
class Address(models.Model):
user = models.ForeignKey(settings.common.AUTH_USER_MODEL, on_delete=models.CASCADE)
currency_names = models.ForeignKey(Currency, on_delete=models.CASCADE)
zebpay = models.CharField(max_length=250, blank=True, null=True)
这是我添加交易所地址的模型。每个地址将保存在相应交易所的表单字段中。
有多种货币,如比特币,以太坊等。当用户单击比特币选项卡时,将显示与之对应的交易所,可以在其中添加和保存表单字段地址。以太坊和其他人也是如此。添加地址并单击“保存”按钮后,将向用户显示添加的地址。
在管理面板上,当管理员点击一个地址时,它将主要显示3件事:-
我能够执行1(因为用户已经登录)和3(在表单字段中添加地址),但是不能将交换添加到已保存的地址。
views.py
class ZebpayView(TemplateView):
template_name = 'currencies/addresses.html'
def get(self, request):
form = ZebpayForm()
addresses = Address.objects.all().order_by('-addition_date')
users = User.objects.exclude(id=request.user.id)
friend = Friends.objects.all().first()
friends = friend.users.all()
context = {'form': form, 'addresses': addresses, 'users': users, 'friends': friends}
return render(request, self.template_name, context)
def post(self, request):
form = ZebpayForm(request.POST)
if form.is_valid():
zebpay = form.save(commit=False)
zebpay.user = request.user
zebpay.save()
text = form.cleaned_data['zebpay'] #data filled in form
form = ZebpayForm() #show blank form again after one input
return redirect('currencies:exchanges')
context = {'form': form, 'text': text}
return render(request, self.template_name, context)
forms.py
class ZebpayForm(forms.ModelForm):
zebpay = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Address'}
))
class Meta:
model = Address
fields = ('zebpay',)
如何添加用户单击并在对应的兑换字段中保存地址的货币名称?
此外,我是否需要在管理面板中添加货币名称或更改货币模型以添加多种货币?
答案 0 :(得分:0)
在货币模型中将您的currency_names作为主键,然后可以用作地址模型中的外键。