Insert One to One field value in django - django

I have the following models.
class PatientInfo(models.Model):
lastname = models.CharField('Last Name', max_length=200)
firstname = models.CharField('First Name',max_length=200)
middlename = models.CharField('Middle Name',max_length=200)
...
def get_absolute_url(self):
return reverse('patient:medical-add', kwargs={'pk': self.pk})
class MedicalHistory(models.Model):
patient = models.OneToOneField(PatientInfo, on_delete=models.CASCADE, primary_key=True,)
...
and upon submitting PatientInfo form it will go to another form which supply the MedicalHistory Details. I can see my PatientInfo data as well as MedicalHistory data but not linked to each other. Below is my MedicalCreateView which process my MedicalHistory form.
class MedicalCreateView(CreateView):
template_name = 'patient/medical_create.html'
model = MedicalHistory
form_class = MedicalForm
def post(self, request, pk):
form = self.form_class(request.POST)
if form.is_valid():
patiente = form.save(commit=False)
physician_name = form.cleaned_data['physician_name'] # do not delete
patient = PatientInfo.objects.all(id=self.kwargs['pk'])
MedicalHistory.patient = self.kwargs['pk']
patiente.save()
messages.success(request, "%s is added to patient list" % physician_name )
return redirect('index')
else:
print(form.errors)
This is how I set MedicalHistory.patient field using the PatientInfo.pk
MedicalHistory.patient = self.kwargs['pk']

If you are using OneToOneField and want to link MedicalHistory to PatientInfo automatically you need to use signals.
class MedicalHistory(models.Model):
patient = models.OneToOneField(PatientInfo, on_delete=models.CASCADE, primary_key=True,)
. . . . .
#receiver(post_save, sender=PatientInfo)
def create_medical_history(sender, instance, created, **kwargs):
if created:
MedicalHistory.objects.create(patient=instance)
#receiver(post_save, sender=PatientInfo)
def save_medical_history(sender, instance, **kwargs):
instance.medicalhistory.save()
Views
class MedicalCreateView(CreateView):
template_name = 'patient/medical_create.html'
model = MedicalHistory
form_class = MedicalForm
success_url = '/'

Related

How to assign model form field to a current logged in user in Django's class based views

I am trying to save a form with the current logged in user's username, but the error "Cannot assign "'Neshno_Games2'": "League.host" must be a "Manager" instance." occurs
Views.py
class CreateLeaguesView(generic.CreateView):
model = League
template_name = "leagues/create-league.html"
form_class = LeaguesCreationForm
success_url = "/leagues/leagues"
def get_context_data(self, **kwargs):
context = super().get_context_data( **kwargs)
context['leagues'] = League.objects.all()
return context
def form_valid(self, form):
manager = self.request.user.username
League.objects.create(
host = manager,
)
return super(CreateLeaguesView, self).form_valid(form)
Model.py
class League(models.Model):
name = models.CharField(max_length=30)
no_players = models.IntegerField(default=20)
start_date = models.DateField(blank=False, null=False)
end_date = models.DateField(blank=False, null=False)
prize = models.CharField(max_length=300)
host = models.ForeignKey(Manager, on_delete=models.CASCADE)
def __str__(self):
return self.name
forms.py
class LeaguesCreationForm(forms.ModelForm):
class Meta:
model = League
fields = (
"name",
"no_players",
"start_date",
"end_date",
"prize",
)
You can try like this:
class CreateLeaguesView(generic.CreateView):
model = League
def form_valid(self, form):
form.instance.host= self.request.user.manager # accessing one to one data
return super().form_valid(form)
More information can be found here in this documentation: https://docs.djangoproject.com/en/4.0/topics/db/examples/one_to_one/

DJANGO - Models.Forms - Usuarios

I'm trying to save and retrive the data owned by the user.
I mean, in one Sqlite3 DB I store the tables for all users but each one has their one data store in it, how can I give each one their own data.
this are my models, view and form
MODEL.PY
class Cuentas (models.Model):
rubro_cta = models.ForeignKey(TipoC, on_delete=models.CASCADE, verbose_name = u'Tipo')
sub_rubro_cta = models.ForeignKey(Sub_rubro, on_delete=models.CASCADE, verbose_name = u'Sub Rubro')
titulo_cuenta = models.CharField(max_length=50)
detalle_cuenta = models.CharField(max_length=60)
importe_cuenta = models.FloatField()
def save(self, *args, **kwargs):
self.importe_cuenta = round(self.importe_cuenta, 2)
super(Cuentas, self).save(*args, **kwargs)
def __str__(self):
return self.detalle_cuenta
FORMS.PY
class CuentasForm (forms.ModelForm):
class Meta:
model = Cuentas
fields = ['rubro_cta', 'sub_rubro_cta', 'detalle_cuenta', 'importe_cuenta']
labels = {
'rubro_cta': _('Cuenta'),
'sub_rubro_cta': _('Tipo'),
'detalle_cuenta': _('Detalle'),
'importe_cuenta': _('Importe'),
}
VIEWS.PY
#login_required
def carga (request):
if request.method == 'POST':
form = CuentasForm(request.POST)
if form.is_valid:
form.save()
return redirect('balance')
else:
form = CuentasForm()
return render (request, "ProyetoWebApp/carga.html",{"form": form})
you have to add a foreign key field to user in your Cuentas class:
class Cuentas (models.Model):
user = models.ForeignKey(User, models.CASCADE)
...
in your view add this code for GET method:
info = None
if request.method == 'GET':
info = Cuentas.objects.get(user=request.user)
return render (request, "ProyetoWebApp/carga.html",{"form": form, "info":info})

Two fields related in Django

I need to update my table every time a new value of "sku" is entered (not to create a new entry), but it does have to happen only if the "client" selected is the same. If the "client" is different, then the model should add a new object with the same "sku", but with different "clients".
I have tried to do the following in my models.py:
class ProductList(models.Model):
id_new = models.IntegerField(primary_key=True)
sku = models.CharField(primary_key=False, max_length=200)
client = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
name = models.CharField(max_length=256)
description = models.CharField(max_length=1000)
storage = models.CharField(max_length=256)
cost_price = models.CharField(max_length=256)
sell_price = models.CharField(max_length=256)
ncm = models.CharField(max_length=256)
inventory = models.IntegerField(null=True)
class Meta:
unique_together = (('sku', 'client'),)
But it is not working. How can I make that work?
You can try like this:
# form
class MyForm(forms.ModelForm):
class Meta:
model = ProductList
def save(self, *args, **kwargs:
client = self.cleaned_data.get('client') # get client from form cleaned_data
if hasattr(self.instance, 'pk') and self.instance.client != client: # check if client match's already existing instance's client
self.instance.pk = None # make a duplicate instance
self.instance.client = client # change the client
return super(MyForm, self).save(*args, **kwargs)
# views.py
# ...
def my_view(request, id):
instance = get_object_or_404(ProductList, id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('next_view')
return render(request, 'my_template.html', {'form': form})
Update
Um you can override the model as well. you can try like this:
# Untested Code but should work
def save(self, *args, **kwargs):
if self.pk:
current_instance = self.__class__.objects.get(pk=self.pk)
if current_instance.client != self.client:
self.pk = None
return super(ProductList, self).save(*args, **kwargs)

Migrate form to modelform

I wrote following form:
class VoteForm(forms.Form):
choice = forms.ModelChoiceField(queryset=None, widget=forms.RadioSelect)
def __init__(self, *args, **kwargs):
question = kwargs.pop('instance', None)
super().__init__(*args, **kwargs)
if question:
self.fields['choice'].queryset = question.choice_set
class VoteView(generic.UpdateView):
template_name = 'polls/vote.html'
model = Question
form_class = VoteForm
def get_queryset(self):
return Question.objects.filter(pub_date__lte=timezone.now()).exclude(choice__isnull=True)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Check duplicate vote cookie
cookie = self.request.COOKIES.get(cookie_name)
if has_voted(cookie, self.object.id):
context['voted'] = True
return context
def get_success_url(self):
return reverse('polls:results', args=(self.object.id,))
def form_valid(self, form):
redirect = super().form_valid(form)
# Set duplicate vote cookie.
cookie = self.request.COOKIES.get(cookie_name)
half_year = timedelta(weeks=26)
expires = datetime.utcnow() + half_year
if cookie and re.match(cookie_pattern, cookie):
redirect.set_cookie(cookie_name, "{}-{}".format(cookie, self.object.id), expires=expires)
else:
redirect.set_cookie(cookie_name, self.object.id, expires=expires)
return redirect
The problem is that the normal form does not represent a object does not have the save() method like ModelForm. But I can't figure out how to migrate the form. There is no choice or choice_set field:
class VoteForm(forms.ModelForm):
class Meta:
Model = Question
#throws exception
fields = ('choice',)
widgets = {
'choice': forms.RadioSelect()
}
EDIT:
Here are the models:
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
How can the form from above be reproduced as a modelform?
Even if you have ModelForm you can define additional fields if you need them. In your case it will be choice field as in previous normal form.
Then in Meta you will exclude all the fields of the Question model which are not required.
After that in init you will provide choice set of provided instance to the choice field.
class VoteForm(forms.ModelForm):
choice = forms.ModelChoiceField(queryset=None, widget=forms.RadioSelect)
class Meta:
model = Question
exclude = ['question_text','pub_date']
def __init__(self, *args, **kwargs):
super(VoteForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance:
self.fields['choice'].queryset = instance.choice_set
The code is written online and not tested, but I think it should work.

Django adding multiple items to one category CheckboxSelectMultiple() using Django Forms

I need to assign multiple CampaignTypes to a Campaign unsing Django FormsModels.
Selecting many CapaignTypes at once, adding the CapaignTypes to only one campaign. Thanks I will appreciate any help
class Campaign(models.Model):
client_id = models.ForeignKey(Company)
name = models.CharField(max_length=45, null=True)
campaign_status = models.ForeignKey(CampaignStatus)
def __str__(self):
return self.name
class Campaign_type(models.Model):
campaign_type = models.CharField(max_length=45)
client_id = models.ForeignKey(Company)
campaign_id = models.ManyToManyField(Campaign, verbose_name='Campaign(s)')
def __str__(self):
return self.campaign_type + ' ' + str(self.client_id)
My code in form.py
class CampaignCampaignTypeForm(forms.ModelForm):
class Meta:
model = CampaignType
exclude = ['campaign_id', 'client_id']
campaign_type = forms.ModelMultipleChoiceField(queryset=CampaignType.objects.all())
def __init__(self, *args, **kwargs):
company = kwargs.pop("company")
if kwargs.get('instance'):
initial = kwargs.setdefault('initial', {})
initial['campaign_type'] = [t.pk for t in kwargs['instance'].campaing_type_set.all()]
forms.ModelForm.__init__(self, *args, **kwargs)
My code in view.py
def add_campaign_type_to_campaign(request, campaign_id):
if not request.user.is_authenticated():
return render(request, 'campaign/login.html')
else:
client_user = ClientUser.objects.get(client=request.user.pk)
form = CampaignCampaignTypeForm(data=request.POST or None, company=client_user.company)
if form.is_valid():
campaigntype = form.save(commit=False).clean()
#client_user = ClientUser.objects.get(client=request.user.pk)
campaign = Campaign.objects.get(id=campaign_id)
campaigntype.campaign_id = campaign
campaigntype.save()
form.save_m2m()
# return render(request, 'campaign/detail_campaign.html', {'campaign_type': campaign_type})
context = {
"form": form,
}
Do you try forms.SelectMultiple widget? Or if you can using Bootstrap on frontend, Select2 is a good JS package to help you on multiple selection.