第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django createView 將當前 user_id 傳遞給 Modelform

Django createView 將當前 user_id 傳遞給 Modelform

倚天杖 2023-09-05 17:29:12
該文件充滿了NUL, '\x00',需要將其刪除。清理行后,用于pandas.DataFrame從 加載數(shù)據(jù)。dimport pandas as pdimport string  # to make column names# the issue is the the file is filled with NUL not whitespacedef import_file(filename):    # open the file and clean it    with open(filename) as f:        d = list(f.readlines())        # replace NUL, strip whitespace from the end of the strings, split each string into a list        d = [v.replace('\x00', '').strip().split(',') for v in d]        # remove some empty rows        d = [v for v in d if len(v) > 2]    # load the file with pandas    df = pd.DataFrame(d)    # convert column 0 and 1 to a datetime    df['datetime'] = pd.to_datetime(df[0] + ' ' + df[1])    # drop column 0 and 1    df.drop(columns=[0, 1], inplace=True)    # set datetime as the index    df.set_index('datetime', inplace=True)    # convert data in columns to floats    df = df.astype('float')    # give character column names    df.columns = list(string.ascii_uppercase)[:len(df.columns)]        # reset the index    df.reset_index(inplace=True)        return df.copy()# call the functiondfs = list()filenames = ['67.csv']for filename in filenames:        dfs.append(import_file(filename))display(df)                       A    B      C    D    E      F     G     H      I     J     K     L    M    N    Odatetime                                                                                                 2020-02-03 15:13:39  5.5  5.8  42.84  7.2  6.8  10.63  60.0   0.0  300.0   1.0  30.0  79.0  0.0  0.0  0.02020-02-03 15:13:49  5.5  5.8  42.84  7.2  6.8  10.63  60.0   0.0  300.0   1.0  30.0  79.0  0.0  0.0  0.02020-02-03 15:13:59  5.5  5.7  34.26  7.2  6.8  10.63  60.0  22.3  300.0   1.0  30.0  79.0  0.0  0.0  0.02020-02-03 15:14:09  5.5  5.7  34.26  7.2  6.8  10.63  60.0  15.3  300.0  45.0  30.0  79.0  0.0  0.0  0.02020-02-03 15:14:19  5.5  5.4  17.10  7.2  6.8  10.63  60.0  50.2  300.0  86.0  30.0  79.0  0.0  0.0  0.0
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經(jīng)驗 獲得超21個贊

在您看來,您應(yīng)該傳遞用戶的主鍵,因此:


? ? def get_form_kwargs(self, *args, **kwargs):

? ? ? ? kwargs = super(CalendarEventAdd, self).get_form_kwargs()

? ? ? ? kwargs['user_id'] = self.request.user.pk

? ? ? ? return kwargs

您可以將其保存在EventForm對象中:


class EventForm(forms.ModelForm):

? ??

? ? def __init__(self, *args, user_id=None, **kwargs):? ?

? ? ? ? super(EventForm, self).__init__(*args, **kwargs)

? ? ? ? self.user_id = user_id

? ? ? ? # input_formats parses HTML5 datetime-local input to datetime field

? ? ? ? self.fields['start_time'].input_formats = ('%Y-%m-%dT%H:%M',)

? ? ? ? self.fields['end_time'].input_formats = ('%Y-%m-%dT%H:%M',)

? ??

? ??

? ? def clean(self, *args, **kwargs):

? ? ? ? form_start_time = self.cleaned_data.get('start_time')

? ? ? ? form_end_time = self.cleaned_data.get('end_time')

? ? ? ? form_manage_id = self.cleaned_data.get('manage_id')

? ? ? ? between = Event.objects.exclude(pk=self.instance.pk).filter(

? ? ? ? ? ? manage_id=self.user_id,

? ? ? ? ? ? end_time__gte=form_start_time,

? ? ? ? ? ? start_time__lte=form_end_time

? ? ? ? )

? ? ? ? if between.exists():

? ? ? ? ? ? raise forms.ValidationError('Already Calendar entry for this time')

? ? ? ? return super().clean()

如果您稍后決定不僅使用 來創(chuàng)建.exclude(pk=self.instance.pk)對象,還使用來更新對象,則 將會排除您正在編輯的對象。EventFormEvent

注意:文檔建議使用AUTH_USER_MODEL設(shè)置 [Django-doc]而不是?get_user_model()[Django-doc]。這更安全,因為如果身份驗證應(yīng)用程序尚未加載,設(shè)置仍然可以指定模型的名稱。因此最好這樣寫:

from django.conf import settings


class Event(models.Model):

? ? # …

? ? manage = models.ForeignKey(

? ? ? ? settings.AUTH_USER_MODEL,

? ? ? ? on_delete=models.CASCADE

? ? )


查看完整回答
反對 回復(fù) 2023-09-05
  • 1 回答
  • 0 關(guān)注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號